我在ASP.NET中有CheckBoxList的应用程序:
<asp:CheckBoxList runat="server" ID="myCheckBoxList">
<asp:ListItem Text="1 16" />
<asp:ListItem Text="1 17" />
<asp:ListItem Text="2 20" />
</asp:CheckBoxList>单击按钮后,我要更改选定元素的样式。要做到这一点,我有JavaScript功能:
function changeColor() {
var checkBoxList = document.getElementById("myCheckBoxList");
var options = checkBoxList.getElementsByTagName('input');
for (var i = 0; i < options.length; i++) {
console.log(options[i].checked);
if (options[i].checked) {
options[i].parentElement.className = 'Red';
}
}
}当我单击按钮时,选定的项目会在很短的时间内改变它们的颜色(大约0,5秒),然后它们返回到默认的黑色。为什么我的复选框列表项目样式重置?我不想这样做。我如何更改我的代码,使其永久地改变颜色(不仅仅是0,5秒)?
发布于 2016-03-21 12:28:42
以下是完整的解决方案:
<script type="text/javascript">
function changeColor() {
var checkBoxList = $('[id$="myCheckBoxList"]');
var options = checkBoxList[0].getElementsByTagName('input');
for (var i = 0; i < options.length; i++) {
console.log(options[i].checked);
if (options[i].checked) {
options[i].parentElement.className = 'Red';
}
}
return false;
}
</script>
<asp:CheckBoxList runat="server" ID="myCheckBoxList">
<asp:ListItem Text="1 16" />
<asp:ListItem Text="1 17" />
<asp:ListItem Text="2 20" />
</asp:CheckBoxList>
<asp:Button OnClientClick="return changeColor();" Text="Test" runat="server" /> 我已经使用myCheckBoxList本地化了jQuery,因为如果您使用母版页,它可以在浏览器中具有不同的Id。其次,我从函数中返回false,以防止回发按钮点击以维护类值。
https://stackoverflow.com/questions/36129717
复制相似问题