javascript 选中一个 Jsp 页面的所有 CheckBox
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7790240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checked all CheckBox of a Jsp page
提问by subodh
I am using Struts1.3, and in this Jsp page dynamically generate check boxes depends on the data available in Database. my code which is generate check boxes is as follow
我使用的是 Struts1.3,在这个 Jsp 页面中动态生成复选框取决于数据库中可用的数据。我生成复选框的代码如下
<table width="850" border="0" align="left" cellpadding="2" cellspacing="0">
<tr>
<td width="100" align="center" bgcolor="#F3F3F3"><label>
<html:checkbox name="ExporterForm" value="<%=authlist.get(i).getAuthorityid()%>" property="exportauthority" styleId="checkbox99" />
</label></td>
<td align="center" class="text_exp" ><%=authlist.get(i).getAuthorityname()%></td>
</tr>
</table>
I have one more check Box(selectAll
), and query is, I want to mark checked all while selection of selectAll CheckBox.
我还有一个复选框(selectAll
),查询是,我想在选择 selectAll CheckBox 时标记所有选中。
and my code is for checked all check box is given below but it's select only one and need to select all please show me the path to achieve it.
并且我的代码用于检查下面给出的所有复选框,但它只选择一个并且需要选择所有请告诉我实现它的路径。
function selectAllAuthorites()
{
var selectAll=document.getElementById("checkbox101")
if(selectAll.checked==true)
{
document.getElementById("checkbox99").checked=true;
}
}
回答by HashimR
You can use getElementsByTagNamemethod of Javascript.
您可以使用Javascript 的getElementsByTagName方法。
This code might be of your help!
此代码可能对您有所帮助!
function checkAll()
{
//alert("Check all the checkboxes...");
var allRows = document.getElementsByTagName("input");
for (var i=0; i < allRows.length; i++) {
if (allRows[i].type == 'checkbox')
{
allRows[i].checked = true;
}
}
}
You can call this function from your html checkbox.
您可以从 html 复选框中调用此函数。
Edit:
编辑:
these links might help you.
这些链接可能对您有所帮助。
javascript-check-one-check-all-checkbox
javascript-check-one-check-all-checkbox
回答by JB Nizet
Only one HTML element in a document may have a given ID. Your page is thus invalid. BTW, that's why the method is name getElementById
(singular) and not getElementsById
(plural).
一个文档中只有一个 HTML 元素可能有一个给定的 ID。您的页面因此无效。顺便说一句,这就是为什么该方法是名称getElementById
(单数)而不是getElementsById
(复数)的原因。
Use document.getElementsByName("ExporterForm")
and iterate through the returned list to check every checkbox of the list.
使用document.getElementsByName("ExporterForm")
并遍历返回的列表以检查列表的每个复选框。