인터넷정보

[스크립트] [펌] 체크박스형으로 디자인 된 멀티 select

알 수 없는 사용자 2008. 2. 4. 11:11
저작권은 명시가 안되어 있네요, 알아서 쓰삼;;;
이거 너무 퍼 날르는 거 아니삼?  (혼자 보기엔 아까워서);;;;

----------=----------------
/**
* multiSelect.js
*
* Converts multiple select form inputs into a DHTML
* version which uses checkboxes instead. This is more
* usable, due to people not knowing how to use a multiple
* select.
*
* The custom selects should end up the same width and height
* more or less as your existing select, so control that with
* css width and height, or the HTML "size" attribute.
*
* Coded by Kae - kae@verens.com
* I'd appreciate any feedback.
* You have the right to include this in your sites.
* Please retain this notice.
*
* Refactored into nice, pretty code by Richard Heyes (http://www.phpguru.org)
*/


var isMsie = document.all ? true : false;


/**
* Adds buildMultipleSelects() to the onload event.
*/
function addEvent(element, event, func)
{
  if (isMsie) {
    element.attachEvent(event, func);

  } else if (element.addEventListener) {
    element.addEventListener(event, func, false);
  }
}


/**
* Handles the conversion of multiple selects
*/
function buildMultipleSelects()
{
  var selectObjects = document.getElementsByTagName('select');

  if (!selectObjects) return;

  var i=0;
  var len= selectObjects.length;  // 무한루프 방지용
  while (i<len) {
    if (selectObjects[i].getAttribute("MULTICHECK") == null) {  // 기존 multiple 살림
      ++i;
      continue;
    }
    --len;

    var ms = selectObjects[i];

    var disabled = ms.disabled ? true : false;
    var width  = ms.style.width; // offsetWidth;
    var height  = ms.style.height; //offsetHeight;

    var divElement      = document.createElement('div');
    divElement.style.overflow = 'auto';
    divElement.style.width  = width;// + "px";
    divElement.style.height  = height;// + "px";
    divElement.style.border  = "2px inset white";
    divElement.style.font = "10pt Arial";
    divElement.className    = 'customMultipleSelect';

    optionObjects = ms.getElementsByTagName('option');

    for (var j=0; j<optionObjects.length; ++j) {
      var spanElement = document.createElement('div');

      spanElement.style.paddingLeft = "20px";
      spanElement.style.cursor = "default";
      spanElement.className = 'customMultipleSelect_option';

      addEvent(spanElement, 'onmousedown', function () {if (isMsie && event.srcElement.tagName.toLowerCase() == 'div' && !event.srcElement.firstChild.disabled) {event.srcElement.childNodes[0].checked = !event.srcElement.childNodes[0].checked;}})

      var inputElement  = document.createElement('input');
      inputElement.type = "checkbox";

      if (optionObjects[j].selected) {
        inputElement.checked    = true;
        inputElement.defaultChecked = true;
      }

      if (disabled) {
        inputElement.disabled = true;
      }

      inputElement.value      = optionObjects[j].value;
      inputElement.style.marginLeft = "-16px";
      inputElement.style.marginTop  = "-2px";
      inputElement.name      = ms.name;

      var textLabel = document.createTextNode(optionObjects[j].text);

      spanElement.appendChild(inputElement);
      spanElement.appendChild(textLabel);
      divElement.appendChild(spanElement);
    }

    ms.parentNode.insertBefore(divElement, ms);
    ms.parentNode.removeChild(ms);
  } // while
}

addEvent(window, isMsie ? 'onload' : 'load', buildMultipleSelects);

--------------=----------multiSelect.js 끝 -----------
<html>
    <head>
        <script src="multiSelect.js" type="text/javascript"></script>

        <style type="text/css">
        <!--
            .customMultipleSelect {
                overflow: auto;
                border: 2px inset white;
            }

            .customMultipleSelect_option {
                border-bottom: 1px solid #eeeeee;
                font: 10pt Tahoma;
                padding: 1px;
                padding-left: 20px;
                width: 100%;
            }
        // -->
        </style>
    </head>
<body>

<select multicheck name="foo[]" style="width: 200px; height: 100px">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3" selected>Option 3</option>
    <option value="4" selected>Option 4</option>
    <option value="5">Option 5</option>
    <option value="6">Option 6</option>
    <option value="7">Option 7</option>
</select>

<br>
기냥 select multiple<br>
<select name="foo3[]" style="width: 200px; height: 100px" size=5 multiple>
    <option value="1">A</option>
    <option value="2">B</option>
    <option value="3">C</option>
    <option value="4">D</option>
    <option value="5">E</option>
    <option value="6">F</option>
    <option value="7">G</option>
</select>

<select name="foo2[]" style="width: 200px; height:110px" multicheck>
    <option value="1">A</option>
    <option value="2">B</option>
    <option value="3">C</option>
    <option value="4">D</option>
    <option value="5">E</option>
    <option value="6">F</option>
    <option value="7">G</option>
</select>


</body>
</html>


출처:PHPSCHOOL 까칠한고민남님
반응형