HTML 코드중에 select, radio, checkBox가 많이 쓰일때 제가 쓰는 함수입니다.


함께 테스트 할수 있는 예제를 같이 써넣을께요.. ^^


----------------------------------------------------------------------------------


<HTML>
<HEAD>
<TITLE> Select, Radio, checkBox </TITLE>
<script language="javascript">
<!--
 function radioButton(formElement)
 {
  alert(formElement.value);
 }


 function checkBox(formElement)
 {
  alert(formElement.value);
 }


 function selectBox(formElement)
 {
  alert(formElement.value);
 }
//-->
</script>
</HEAD>


<BODY>
<%=selectBox("selectHobby", "영화감상,음악감상,골프,바둑", "음악감상", "onchange='selectBox(this)'")%>
<%
 response.write vbcr & "<br><br>" & vbcr


 response.write radioButton("radioHobby", "영화감상,음악감상,골프,바둑", "골프", "onclick='radioButton(this)'")


 response.write vbcr & "<br><br>" & vbcr


 response.write checkBox("checkBoxHobby", "영화감상,음악감상,골프,바둑", "바둑", "onclick='checkBox(this)'")


 'selectbox
 function selectBox(name, value, selectValue, javaScript)
  dim sValue, i, totalView


  totalView = "<select name='" & name & "' " & javaScript & ">" & vblf


  sValue = split(value, ",")


  for i = 0 to ubound(sValue)
   totalView = totalView & "<option value='" & sValue(i) & "'"


   if sValue(i) = selectValue then
    totalView = totalView & " selected"
   end if


   totalView = totalView & ">" & sValue(i) & "</option>" & vblf
  next


  totalView = totalView & "</select>"


  selectBox = totalView
 end function


 'radio
 function radioButton(name, value, selectValue, javaScript)
  dim sValue, i, totalView


  sValue = split(value, ",")


  for i = 0 to ubound(sValue)
   totalView = totalView & "<input type='radio' name='" & name & "' value='" & sValue(i) & "'"


   if sValue(i) = selectValue then
    totalView = totalView & " checked"
   end if


   if not isnull(javaScript) or javaScript <> "" then
    totalView = totalView & " " & javaScript
   end if


   totalView = totalView & ">" & sValue(i) & "&nbsp;" & vblf
  next


  radioButton = totalView
 end function


 'checkbox
 function checkBox(name, value, selectValue, javaScript)
  dim sValue, i, totalView


  sValue = split(value, ",")


  for i = 0 to ubound(sValue)
   totalView = totalView & "<input type='checkbox' name='" & name & "' value='" & sValue(i) & "'"


   if sValue(i) = selectValue then
    totalView = totalView & " checked"
   end if


   if not isnull(javaScript) or javaScript <> "" then
    totalView = totalView & " " & javaScript
   end if


   totalView = totalView & ">" & sValue(i) & "&nbsp;" & vblf
  next


  checkBox = totalView
 end function
%>
</BODY>
</HTML>




----------------------------- 수정 ------------------------------


아무래도 사용을 하다보니 한계가 느껴지네요.. ㅋㅋ


그래서 몇가지더 수정을 해서 올립니다. 다만 이런 Function을 사용하는건..


회원가입 혹은 입사지원 같은 폼 형태에서 사용하시는게 좋습니다.


일반 페이지에서 이걸 쓴다면.. -_- 몇개 안되시면 그냥 HTML에 적용하시는게 빠를수도 있습니다. ^^




<%
 '----------------------------------------------------------------------------------
 ' selectBox 만들기
 '  - name : select 이름
 '  - value : option의 value값 형태는("1,2,3")
 '  - title : option의 title값 형태는("1,2,3")
 '  - selectValue : 선택된 값
 '  - style : style 적용값
 '  - vClass : css 값
 '  - javascript : javascript 내용
 '----------------------------------------------------------------------------------
 Function selectBox(name, value, title, selectValue, style, vClass, javascript)
  sValue = split(value, ",")


  if title <> "" then
   sTitle = split(title, ",")
  end if


  write = "<select name='" & name & "' "


  if style <> "" then
   write = write & " style='" & style & "' "
  end if
 
  if vClass <> "" then
   write = write & " class='" & vClass & "' "
  end if


  if javascript <> "" then
   write = write & javascript
  end if


  write = write & ">"


  for i = 0 to ubound(sValue)
   write = write & "<option value='" & sValue(i) & "'"


   if sValue(i) = selectValue then
    write = write & " selected"
   end if


   write = write & ">"


   if title <> "" then
    write = write & sTitle(i)
   else
    write = write & sValue(i)
   end if


   write = write & "</option>"
  next
 
  write = write & "</select>"


  Response.Write write
 End Function



 '----------------------------------------------------------------------------------
 ' radio 만들기
 '  - name : radio 이름(공통)
 '  - value : value값 형태는("1,2,3")
 '  - title : title값 형태는("1,2,3")
 '  - checkValue : 체크된 값
 '  - alignNum : 정렬시 사용되는 숫자
 '  - style : style 적용값
 '  - vClass : css 값
 '  - javascript : javascript 내용
 '----------------------------------------------------------------------------------
 Function radio(name, value, title, checkValue, alignNum, style, vClass, javascript)
  sValue = split(value, ",")


  if title <> "" then
   sTitle = split(title, ",")
  end if


  for i = 0 to ubound(sValue)
   write = write & "<input type='radio' name='" & name & "' value='" & sValue(i) & "'"


   if sValue(i) = checkValue then
    write = write & " checked"
   end if


   if style <> "" then
    write = write & " style='" & style & "'"
   end if
   
   if vClass <> "" then
    write = write & " class='" & vClass & "'"
   end if


   if javascript <> "" then
    write = write & javascript
   end if


   write = write & ">"


   if title <> "" then
    write = write & sTitle(i)
   else
    write = write & sValue(i)
   end if


   if alignNum <> "" then
    if (i+1) mod alignNum = 0 then
     write = write & "<br>"
    end if
   end if
  next


  Response.Write write
 End Function



 '----------------------------------------------------------------------------------
 ' checkbox 만들기
 '  - name : radio 이름(공통)
 '  - value : value값 형태는("1,2,3")
 '  - title : title값 형태는("1,2,3")
 '  - checkValue : 체크된 값
 '  - alignNum : 정렬시 사용되는 숫자
 '  - style : style 적용값
 '  - vClass : css 값
 '  - javascript : javascript 내용
 '----------------------------------------------------------------------------------
 Function checkBox(name, value, title, checkValue, alignNum, style, vClass, javascript)
  sValue = split(value, ",")


  if title <> "" then
   sTitle = split(title, ",")
  end if


  for i = 0 to ubound(sValue)
   write = write & "<input type='checkbox' name='" & name & "' value='" & sValue(i) & "'"


   if instr(checkValue, sValue(i)) > 0 then
    write = write & " checked"
   end if


   if style <> "" then
    write = write & " style='" & style & "'"
   end if
   
   if vClass <> "" then
    write = write & " class='" & vClass & "'"
   end if


   if javascript <> "" then
    write = write & javascript
   end if



   write = write & ">"


   if title <> "" then
    write = write & sTitle(i)
   else
    write = write & sValue(i)
   end if


   if alignNum <> "" then
    if (i+1) mod alignNum = 0 then
     write = write & "<br>"
    end if
   end if
  next


  Response.Write write
 End Function
%>
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>


<BODY>
 <%call selectBox("testSelect", "1,2,3,4,5", "", "3", "", "", "")%>
 <br><br>
 <%call radio("testRadio", "1,2,3,4,5", "", "3", "3", "", "", "")%>
 <br><br>
 <%call checkBox("testCheck", "1,2,3,4,5", "", "1,2", "3", "", "", "")%>
</BODY>
</HTML>

2009/05/26 13:43 2009/05/26 13:43

Trackback Address :: https://youngsam.net/trackback/501