JS,AJAX操作DropDownList的一些整理

javascript操作DropDownList:
<script type="text/javascript">
        //获取DropDownList控件
        var dropDownList = document.getElementById("<%=DropDownList1.ClientID %>");
        //读取DropDownList选中项的value
        function getDDLValue() {
            var selValue = document.getElementById("<%=DropDownList1.ClientID %>").options[document.getElementById("<%=DropDownList1.ClientID %>").selectedIndex].value;
        }

        //读取DropDownList选中项的text
        function getDDLText() {
            var selText = document.getElementById("<%=DropDownList1.ClientID %>").options[document.getElementById("<%=DropDownList1.ClientID %>").selectedIndex].text;
        }

        //根据Text值设置选中某项
        function setDDlByText(text) {
            for (i = 0; i < document.getElementById("<%=DropDownList1.ClientID %>").options.length; i++) {
                if (document.getElementById("<%=DropDownList1.ClientID %>").options[i].text == text) {
                    document.getElementById("<%=DropDownList1.ClientID %>").options[i].selected = true;
                }
            }
        }

        //根据value值设置选中某项 
        function setDDlByValue(value) {
            document.getElementById("<%=DropDownList1.ClientID %>").value = value;
        }

        //给DropDownList添加新项
        function addDDLItem(text) {
            var tOption = document.createElement("Option");
            tOption.text = text;
            tOption.value = document.getElementById("<%=DropDownList1.ClientID %>").options.length + 1;
            document.getElementById("<%=DropDownList1.ClientID %>").add(tOption);
        }
    </script>

AJAX无刷新联动多个DropDownList:

<script src="JS/divShow/jquery.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
          $(function() {
              var $ddl = $("select[name$=ddlEmployee]");
              var $ddlCars = $("select[name$=ddlEmployeeCars]");
              $ddl.focus();
              $ddl.bind("change keyup", function() {
                  if ($(this).val() != "0") {
                      loadEmployeeCars($("select option:selected").val());                    
                      $ddlCars.fadeIn("slow");
                  } else {
                      $ddlCars.fadeOut("slow");
                  }
              });
          });

          function loadEmployeeCars(selectedItem) {
              $.ajax({
                  type: "POST",//post方式不能传输json格式的数据,反复测试过,不知道这个结论对不对
                  url: "GetInsTwo.ashx?id=" + selectedItem,//context.Request["id"].Trim()
                  async: true,
                  success: function Success(data) {
                      printEmployeeCars(data);
                  },
                  error: function (msg) {
                      alert(msg.responseText)
                  }
              });
          }        
   
          function printEmployeeCars(data) {
              $("select[name$=ddlEmployeeCars] > option").remove();
              var list = data.split(";");
              for (var i = 0; i < list.length - 1; i++) {
                  $("select[name$=ddlEmployeeCars]").append(
                      $("<option></option>").val(list[i].substring(0, list[i].indexOf(','))).html(list[i].substring(list[i].indexOf(',') + 1, list[i].length))//这一步要根据你后台ahsx返回的数据格式进行处理,我这里返回的是字符串“1,a;2,b;3,c;...”这样的格式
                  );
              }
          }       
</script>
原文地址:https://www.cnblogs.com/hopedilei/p/3486899.html