• 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>
  • 相关阅读:
    (译) 在AngularJS中使用的表单验证功能
    TypeError: Cannot red property 'style' of null 错误解决
    Velocity CheckingForNull
    推荐系统(Recommendation system )介绍
    python 时间戳和日期相互转换
    Markdown 语法的简要规则
    mpvue + 微信小程序 picker 实现自定义多级联动 超简洁
    微信小程序 上传图片并等比列压缩到指定大小
    vue路由的两种模式,hash与history的区别
    修改浏览器url地址或者参数 , 实现不刷新页面
  • 原文地址:https://www.cnblogs.com/hopedilei/p/3486899.html
Copyright © 2020-2023  润新知