zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    《三体》推荐
    低调做人,高调做事
    注意力的培养是学校教学的真正目的
    【RTP.NET入门系列 一】接收第一个RTP包。
    MapX开发日记(三)GPS项目终于有了眉头
    【RTP.NET入门系列 二】接收第一个RTP帧。
    10.04 flash 乱码 问题
    10.04 中文输入发问题。
    通过值类型进行Timer类的线程的同步。
    关于ManualResetEvent信号机制。
  • 原文地址:https://www.cnblogs.com/hopedilei/p/3486899.html
Copyright © 2011-2022 走看看