的Javascript检查所有复选框在表/ asp.net [英] Javascript check all checkboxes in a table / asp.net
问题描述
我有在行的表,每一行有几个< TD>
元素。最后一个元素有一个复选框。
他们是在< DIV>
被设置为 =服务器
。我有一个名为chkAll页面上的其他复选框,单击时,我想的JavaScript选中或取消选中所有复选框在我的表。
I have a table with rows in, and each row has a few <td>
elements. The last element has a checkbox in.
They are in a <div>
which is set to runat="server"
. I have another checkbox on the page called "chkAll" that when clicked, I want to javascript check or uncheck all of the checkboxes in my table.
我不在的Javascript非常好,所以我不知道该怎么做。我加了一个javascript的onclick方法,并提出的document.getElementById并放入div.clientID,但我不是知道该怎么从那里做。任何想法?
I'm not very good at Javascript, so I am not sure what to do. I added a javascript onclick method, and put document.getelementbyid and put in the div.clientID, but I wasnt sure what to do from there. Any ideas?
推荐答案
一旦你拥有了&LT; DIV&GT;
元素作为参考,使用的getElementsByTagName()
来获得&LT;输入&GT;
元素,然后检查键入
属性,如果它是一个复选框,然后将其设置的检查
属性相同检查
复选框的财产 chkAll
。例如:
once you have the the <div>
element as a reference, use getElementsByTagName()
to get the <input>
elements, then check the type
property, if it's a checkbox then set it's checked
property the same as the checked
property of the checkbox chkAll
. For example
function toggleCheckBoxes(elem) {
var div = document.getElementById('<% = divid.ClientID %>');
var chk = div.getElementsByTagName('input');
var len = chk.length;
for (var i = 0; i < len; i++) {
if (chk[i].type === 'checkbox') {
chk[i].checked = elem.checked;
}
}
}
,然后附加此功能为 chkAll
元素的Click事件处理程序
and then attach this function as a click event handler of the chkAll
element
<input type="checkbox" id="chkAll" onclick="toggleCheckBoxes(this)" />
下面是一个 工作演示 。添加 /修改的URL看code
Here's a Working Demo. add /edit to the URL to see the code
这篇关于的Javascript检查所有复选框在表/ asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!