Asp.net如何在获取javascript函数时定义下拉列表ID [英] Asp.net how to define the dropdownlist ID in getting javascript function
问题描述
我有2个Dropdownlist,我想使用我的dropdownlist ID在ASP.Net中调用这个Javascript函数。我可以得到这个值,javascript函数是从下拉列表中禁用所选项目所以它不能被选择到其他下拉列表.....
..........这是我从互联网上获取的脚本标签我在这里粘贴了我的下拉列表ID工作,但'如果我将值保存到数据库,它重置,我想我没有以正确的方式调用DropDownlist ID可以有人帮助我。关于如何以javascript,asp.net的方式调用DropDownList ID,谢谢。
I have 2 Dropdownlist and I want to call this Javascript function in ASP.Net using my dropdownlist ID.how can I get this value, The javascript function is disabling the selected item from the dropdownlist so it cannot be selected to other dropdownlist.....
..........this the script tag that I get from the internet Ive paste my dropdownlist ID in here it is working but' if I save the value to database it resets, I think I'm not calling the DropDownlist ID in the right way can someone help me. on how to call the DropDownList ID in the javascript, asp.net way, thank you.
<asp:DropDownList ID = "ddlHidden1" runat="server" >
<asp:ListItem value="0">Select
<asp:ListItem value="1">1
<asp:ListItem value="2">2
<asp:ListItem value="3">3
<asp:ListItem value="4">4
<asp:ListItem value="5">5
</asp:DropDownList>
<asp:DropDownList ID = "ddlHidden2" runat="server">
<asp:ListItem value="0">Select
<asp:ListItem value="1">1
<asp:ListItem value="2">2
<asp:ListItem value="3">3
<asp:ListItem value="4">4
<asp:ListItem value="5">5
</asp:DropDownList>
<asp:Button ID=btnSave runat="server" Text="Save" OnClick="btnSave_Click" />
<script type=text/javascript>
var ids = ['ddlHidden1', 'ddlHidden2'];
NodeList.prototype.forEach = HTMLCollection.prototype.forEach =
Array.prototype.forEach;
var selected = [];
ids = makeid(ids);
var opts = find(ids);
updateS(opts);
massdisabler(opts);
assignFunc(ids, disabler);
function assignFunc(i, func) { //done
var x;
for (x = 0; x < i.length; ++x) {
i[x].onchange = function () {
func(this);
};
}
}
function disabler(i) {
console.log(selected);
updateS(opts);
massdisabler(opts);
}
function makeid(i) { //done
var o = [];
i.forEach(function (v) {
o.push(document.getElementById(v))
});
return o;
}
function find(ids) { //done
var o = {};
for (var i = 0; i < ids.length; ++i) {
o[i] = {};
ids[i].children.forEach(function (v, n) {
o[i][n] = v;
});
}
return o;
}
function massdisabler(op) { //done
var y = Object.keys(op).length;
for (var x = 0; x < y; x++) {
var t = 0,
l = Object.keys(opts[x]).length;
for (var v = 0; v < l; v++) {
t = op[x][v];
if (selected.some(function (k) {
if (t.value == k) {
return true;
}
})) {
t.setAttribute("disabled", "disabled");
} else {
t.removeAttribute("disabled");
}
}
}
}
function updateS(op) { //done
var s = [],
y = Object.keys(op).length;
for (var x = 0; x < y; x++) {
var t = 0,
l = Object.keys(opts[x]).length;
for (var v = 0; v < l; v++) {
t = op[x][v];
var yn = s.some(function (k) {
if (t.value == k) {
return true;
}
});
if (t.selected && (!yn)) s.push(t.value);
}
}
selected = s;
}
</script>>
推荐答案
.NET Framework中的ASP.NET内联表达式简介 [ ^ ]
Introduction to ASP.NET inline expressions in the .NET Framework[^]
<script type="text/javascript">
var ids = ['<%=ddlHidden1.ClientID %>', '<%=ddlHidden2.ClientID %>'];
</script>
这篇关于Asp.net如何在获取javascript函数时定义下拉列表ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!