首页 >web前端 >js教程 >javascript如何操作服务器控件代码实例详解

javascript如何操作服务器控件代码实例详解

伊谢尔伦
伊谢尔伦原创
2017-07-24 10:24:311487浏览

js选中服务器控件select与dropdownlist

1. js操作服务器控件select

<select id="selectID" onchange="return showMessage()">
     <option value="0">==请选择==</option>
     <option value="1">是</option>
     <option value="2">否</option>
</select>
<script type="text/javascript" language="javascript">
  function showMessage() {
      if (document.getElementById("selectID").options[document.getElementById("selectID").selectedIndex].value == 1) {
   alert("你好,你选择了第 1 个");
   document.getElementById("txtContractName").setAttribute("enable",false);
      }
      else if (document.getElementById("selectID").options[document.getElementById("selectID").selectedIndex].value == 2) {
   alert("你好,你选择了第 2 个");
      }
  }
</script>
//js操作服务器控件dropdownlist
<asp:DropDownList ID="ddlFolder" runat="server" SkinID="ddlSkin" AutoPostBack="false" OnSelectedIndexChanged="ddlFolder_SelectedIndexChanged">    
        <asp:ListItem Value="0">选项0</asp:ListItem>   
   <asp:ListItem Value="1">选项1</asp:ListItem>  
</asp:DropDownList>    
<asp:DropDownList ID="ddlFolder" runat="server" SkinID="ddlSkin" AutoPostBack="false" OnSelectedIndexChanged="ddlFolder_SelectedIndexChanged"> 
  <asp:ListItem Value="0">选项0</asp:ListItem>
  <asp:ListItem Value="1">选项1</asp:ListItem> 
</asp:DropDownList>

JS代码:

document.getElementById("ddlFolder").value="0";//0为你要选中的项的value

2. 根据Text值设置选中某项

var DropDownListCurrencyNew =  document.getElementById("ddlFolder");
for(i = 0; i < DropDownListCurrencyNew.options.length; i++)
{
          if(DropDownListCurrencyNew.options[i].text == "选项0")    
           {
               DropDownListCurrencyNew.options[i].selected = true; 
          }
}

js读取DropDownList选中项的value和text

Value:

var selValue = document.getElementById("DropDownList1").options[document.getElementById("DropDownList1").selectedIndex].value;

Text:

var selText = document.getElementById("DropDownList1").options[document.getElementById("DropDownList1").selectedIndex].text;


以上是javascript如何操作服务器控件代码实例详解的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn