足球游戏_中国足彩网¥体育资讯$

JS简单操作select和dropdownlist实例
来源:易贤网 阅读:1829 次 日期:2014-11-28 10:46:41
温馨提示:易贤网小编为您整理了“JS简单操作select和dropdownlist实例”,方便广大网友查阅!

本文实例讲述了JS简单操作select和dropdownlist的方法。分享给大家供大家参考。具体实现方法如下:

一、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;

希望本文所述对大家的js与.net程序设计有所帮助。

中国足彩网信息请查看IT技术专栏

中国足彩网信息请查看脚本栏目
易贤网手机网站地址:JS简单操作select和dropdownlist实例
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 新媒体/短视频平台 | 手机站点

版权所有:易贤网