字段长度不适用于单个复选框

Field.length not working on a single checkbox

本文关键字:单个 复选框 适用于 不适用 段长度 字段      更新时间:2023-09-26

我有一个包含数据列表的表单,我在每个表单中添加了一个复选框,当我单击最上面的复选框时,它会选择所有数据。但如果我只有一行数据,并且我点击了最上面的复选框,那就不起作用了。这是下面的脚本。

////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检查

祝你好运!