使用 Javascript 循环遍历复选框
我尝试使用在这里和其他地方找到的 JavaScript 示例。问题是我正在使用在服务器上工作的表格控件,因此我使用的 JavaScript 也无法识别服务器端的复选框。我一遍又一遍地收到相同的错误消息。我有 20 行,每行 2 列,一列用于复选框,一列用于用户必须通过选中该框进行验证才能提交的语句。用户必须亲自检查每个框。如果未选中任何复选框,那么我希望看到一条弹出消息,说明他们必须选中所有框,否则我将重定向到另一个页面,当然所有这些都在按钮单击事件上。这一切都在Visual Studio 2010上,使用C#,后端是SQL Server。
这是我使用的 JS 示例:
function checkCheckBoxes() {
if (document.frmTest. CheckBox1.checked == false &&
document.frmTest. CheckBox2.checked == false &&
document.frmTest. CheckBox3.checked == false &&
document.frmTest. CheckBox4.checked == false &&
document.frmTest. CheckBox5.checked == false &&)
{
alert ('You must check all the checkboxes!');
return false;
}else{
return true;
}
}
然后,
<form onsubmit="return checkCheckBoxes();" action="">
<input type="checkbox" name=" CheckBox1" value="1">
<input type="checkbox" name=" CheckBox2" value="2">
<input type="checkbox" name=" CheckBox3" value="3">
<input type="checkbox" name=" CheckBox4" value="4">
<input type="checkbox" name=" CheckBox5" value="5">
<input type="submit" value="Submit!" />
</form>
但我意识到复选框不能是服务器控件,而是 JavaScript 控件。谢谢。
I tried using JavaScript samples that I found here and other places. The problem is that I am using a Table control working at the server, so the JavaScript I was using does not recognizes the checkboxes which are server side as well. I kept getting the same error message over and over. I got 20 rows with 2 columns each, one for the checkbox and one for a statement that the user has to validate by checking the box before they can submit. The user has to physically check each box. If any checkbox is not checked, then I would like to sees a popup message saying they must check all boxes, Else I am doing a redirect to another page, of course all this on the button click event. This all on Visual Studio 2010, using C#, the back-end is SQL Server.
Here a sample of the JS I was using:
function checkCheckBoxes() {
if (document.frmTest. CheckBox1.checked == false &&
document.frmTest. CheckBox2.checked == false &&
document.frmTest. CheckBox3.checked == false &&
document.frmTest. CheckBox4.checked == false &&
document.frmTest. CheckBox5.checked == false &&)
{
alert ('You must check all the checkboxes!');
return false;
}else{
return true;
}
}
then,
<form onsubmit="return checkCheckBoxes();" action="">
<input type="checkbox" name=" CheckBox1" value="1">
<input type="checkbox" name=" CheckBox2" value="2">
<input type="checkbox" name=" CheckBox3" value="3">
<input type="checkbox" name=" CheckBox4" value="4">
<input type="checkbox" name=" CheckBox5" value="5">
<input type="submit" value="Submit!" />
</form>
But I realized that the checkboxes can't be server control but JavaScript controls. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你需要这样的东西
希望这会有所帮助。
You need something like this
Hope this helps.
您可以使用 C# ClientID 属性获取复选框的 HTML ID。将该 ID 插入到您的 Javascript 中,然后您就可以选择相关的复选框并对其进行任何您喜欢的操作。
You can get the HTML ID of the checkboxes by using the C# ClientID property. Insert that ID into your Javascript, and you will then be able to select the relevant checkboxes and do whatever you like with them.
使用 querySelectorAll 检查任何未选中的复选框。如果有,则抛出错误消息,否则回发。
注意:这仅适用于现代浏览器 Firefox 3.1+、IE8+(仅在 IE8 标准模式下)和 Safari 3.1+
Use querySelectorAll to check for any unchecked check boxes. If there are any, throw the error message, else postback.
Note: this will only work in Modern browsers Firefox 3.1+, IE8+ (only in IE8 standards mode), and Safari 3.1+