admin管理员组文章数量:1405542
In my apsx page, I have a listbox (techGroups) that has some items that are preselected. The user can change the selections. Meanwhile, I have a reset button. When the user click the reset button, the listbox will be restored with those preselected items selected, while others are not.
I write following javascript function for the reset button's onclientclick. Somehow, after i click the reset button, only the first preselected item get selected, all other preselected items are not.
reset()
{
var selectedGroups = hiddenfield1.value.split(","); //i saved those preselected items in a hiddenfield
for (var i = 0; i < techGroups.options.length; i++) {
for (var j = 0; j < selectedGroups.length; j++) {
if (techGroups.options[i].value == selectedGroups[j]) {
techGroups.options[i].selected = true;
}
}
}
}
Can anybody help me to look at my code and tell me what is wrong? Thanks.
In my apsx page, I have a listbox (techGroups) that has some items that are preselected. The user can change the selections. Meanwhile, I have a reset button. When the user click the reset button, the listbox will be restored with those preselected items selected, while others are not.
I write following javascript function for the reset button's onclientclick. Somehow, after i click the reset button, only the first preselected item get selected, all other preselected items are not.
reset()
{
var selectedGroups = hiddenfield1.value.split(","); //i saved those preselected items in a hiddenfield
for (var i = 0; i < techGroups.options.length; i++) {
for (var j = 0; j < selectedGroups.length; j++) {
if (techGroups.options[i].value == selectedGroups[j]) {
techGroups.options[i].selected = true;
}
}
}
}
Can anybody help me to look at my code and tell me what is wrong? Thanks.
Share Improve this question edited Oct 14, 2011 at 2:56 Larry Morries 6657 silver badges17 bronze badges asked Oct 14, 2011 at 2:45 GLPGLP 3,68520 gold badges65 silver badges97 bronze badges 1- Thanks for your help. I found out it is because a typo calling the split, I should put split(", "), I missed a space. – GLP Commented Oct 14, 2011 at 13:56
2 Answers
Reset to default 1jQuery allowed? if so please see it working here (if not, please disconsider):
http://jsfiddle/sW8HX/4/
May be reference issue (DOM). Try this,
JavaScript:
<script type="text/javascript">
window.onload = function () {
var btnReset = document.getElementById("btnReset");
btnReset.onclick = function () {
var hid1 = document.getElementById("hiddenField1");
var techGroups = document.getElementById("techGroups");
var selectedGroups = hid1.value.split(","); //i saved those preselected items in a hiddenfield
for (var i = 0; i < techGroups.options.length; i++) {
for (var j = 0; j < selectedGroups.length; j++) {
if (techGroups.options[i].value == selectedGroups[j]) {
techGroups.options[i].selected = true;
}
}
}
};
};
</script>
Markup:
<form id="form1" runat="server">
<div>
<input type="hidden" id="hiddenField1" value="aa,bb,cc" />
<select id="techGroups" size="4" multiple="multiple">
<option value="rr">rr</option>
<option value="aa">aa</option>
<option value="cc">cc</option>
<option value="zz">zz</option>
<option value="bb">bb</option>
<option value="dd">dd</option>
</select>
<input type="button" id="btnReset" value="Reset" />
</div>
</form>
本文标签: javascripthow to set the items selected in a listbox with values provided by an arrayStack Overflow
版权声明:本文标题:javascript - how to set the items selected in a listbox with values provided by an array? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744310353a2599996.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论