字段长度不适用于单个复选框
Field.length not working on a single checkbox
我有一个包含数据列表的表单,我在每个表单中添加了一个复选框,当我单击最上面的复选框时,它会选择所有数据。但如果我只有一行数据,并且我点击了最上面的复选框,那就不起作用了。这是下面的脚本。
////checking all checkbox on page
function checkAll(field)
{
// alert(field.length);
for (i = 0; i < field.length; i++){
field[i].checked = true ;
}
}
////unchecking all checkbox on page
function uncheckAll(field)
{
for (i = 0; i < field.length; i++){
field[i].checked = false;
}
}
function selectCHK(e){
if(e.checked==true){
//alert("Yeah");
checkAll(document.paginate_list.list);
}else{
uncheckAll(document.paginate_list.list);
}
}
function ApplyToSelected(){
var SelectedVal="";
var withSelected=document.getElementById("withSelected");
//
if(withSelected.value==1){
//if(document.getElementById("list")){
// SelectedVal=document.getElementById("list").value;
//}
for(i=0; i<document.paginate_list.list.length;i++){
if (document.paginate_list.list[i].checked) {
//alert(i);
SelectedVal +=document.paginate_list.list[i].value+",";
}
}
if(SelectedVal==""){
return false;
}
if(confirm("Are you Sure you want to Delete the Selected Row?")){
$.ajax({
url: "../__lib__/pagingLib.php?tableDeleteID="+SelectedVal,
context: document.body,
success: function(response,status,xhr){
if(response==1){
alert("Deleted Successfully");
window.location.reload();
}else{
alert(response);
alert("An Error Occurred while Deleting, Please Try Again!");
}
// alert(response);
}
});
}
}
}
HTML数据
<form name="paginate_list" style="display:inline;" action="" method="post">
<table class="PagingBody">
<tr>
<th><input type=checkbox onclick=selectCHK(this) ></th>
<th>Category Title</th><th>Actions</th>
</tr>
<tr class="even">
<td><input name=list[] type=checkbox id=list value=10></td>
<td>Web Design</td>
<td><a href=?page=cat&edit=10>Edit</a> | <a href=#10>Delete</a></td>
<tr/>
</table>
<table>
<tr>
<td>
<select name="withSelected" id="withSelected">
<option value="0">With Selected</option>
<option value="1">Delete</option>
</select>
<input type="button" name="applyToSelected" id="applyToSelected" value="Apply to Selected" onclick="ApplyToSelected()" />
</td>
<tr/>
</table>
</form>
paginate_list.list
表示我的表单名称和列表名称。
这个小提琴将演示正在发生的事情
小提琴在这里
之所以会发生这种情况,是因为当您发送给函数checkAll();
的列表中有多个元素时,浏览器会生成NodeList否则,您只得到正常的输入元素。
我添加了一个instanceof
检查来查看它是什么类型的对象。
要在IE中检查这一点,请在SO上点击此线程:IE 中的NodeList检查
祝你好运!
相关文章:
- 使用jquery选中/取消选中单个复选框
- 获取带有textarea的复选框,并在单个jquery中进行选择
- 单个复选框的 JavaScript 验证在 Perl CGI 中失败
- 应用按钮集后禁用单个复选框
- 在单个按钮上选中所有和取消选中所有不适用于单个复选框.请给我解决方案
- jQuery / JavaScript 复选框显示多个和单个选择中选中的复选框的数量
- 复选框选中功能,用于单个页面中的两组复选框,不能单独调用功能
- Extjs使用复选框模型在网格上拖放单个项目
- 字段长度不适用于单个复选框
- 单个复选框的javascript中的复选框验证失败
- 如何使用ng-repeat从包含5行的表传递单个复选框值
- 启用基于单选按钮选中的单个或多个复选框
- 单击单个复选框时选择表中的所有复选框
- 计算单个复选框被选中的次数
- 启用多个文本框和单个复选框
- 选择单个复选框时,选中其他复选框
- 将数组中的单个复选框值发送到服务器
- 如何设置单个复选框来切换/取消切换其他复选框
- 如何遍历Div并提取单个复选框值
- Parsley.js单个复选框未被验证