简体   繁体   English

用JavaScript选中的复选框不发布

[英]Checkbox checked with javascript does not POST

I have an array of 27 checkboxes generated using php. 我有一个使用php生成的27个复选框的数组。 Checking the checkboxes manually works perfectly. 手动选中复选框非常有效。 The checkboxes are placed in an html table (other tabular dated is included), (all inside the form tags) with name="chk[]' and value='Index Number' of the array (0-27) in the usual form of 复选框放置在html表中(包括其他带有表格的日期),(均在表单标签内),其名称为“ =“ chk []'”且值=“索引号”为数组的常规格式(0-27)的

When I execute a typical Java-script "Check All" function (from a link_click or button_click) the check boxes all display the check-boxes as 'checked'. 当我执行典型的Java脚本“全部检查”功能(通过link_click或button_click)时,所有复选框均将复选框显示为“已选中”。 But, nothing gets submitted. 但是,什么都没有提交。 Print_r shows the 'chk' array is indeed empty when I submit! Print_r显示'chk'数组在我提交时确实为空!

If I set the check-boxes manually, $_POST('chk') array IS posted, and everything works as expected. 如果我手动设置复选框,则会发布$ _POST('chk')数组,一切都会按预期进行。

It appears that when checking the check-boxes with Javascript, ¡checked' values don't get posted. 似乎在使用Java复选框选中时,“ checked”值不会被发布。

Print_r confirms the 'chk' array is empty whenever the 'displayed' value was set using Javascript! 每当使用Javascript设置“显示”值时,Print_r都会确认“ chk”数组为空!

Can anybody attempt to explain to me why the 'displayed value' of the checkbox is not reflected by, or included in the post? 任何人都可以尝试向我解释为什么复选框的“显示值”没有反映或包含在帖子中吗?

The page validates on W3C Ok and I have crawled over my code and cannot find any likely errors. 该页面已在W3C Ok上验证,并且我已爬网我的代码,并且找不到任何可能的错误。 Platform is Win7/Wamp Sever/Firefox. 平台是Win7 / Wamp Sever / Firefox。 Google/StackOverflow search does not reveal any similar symptoms/solutions. Google / StackOverflow搜索不会显示任何类似的症状/解决方案。

Many thanks in advance for anyone with an idea on the problem. 非常感谢任何对这个问题有想法的人。

The javascript CheckAll function I am using is - 我正在使用的JavaScript CheckAll函数是-

function checkall(frm) {
  for (var i=0; i<frm.elements.length; i++) {
  if (frm.elements[i].name = "chk")
  {frm.elements[i].checked = true;}
  }
  }   

The array of 27 check-boxes is in the normal array format, with 'value' being the array Index. 27个复选框的数组为常规数组格式,其中“ value”为数组索引。

<tr><td><input type='checkbox' title='' name='chk[]' value='6' ></td> text label </tr>

You have two errors in your if condition. 如果条件中有两个错误。

  1. It's not even a condition, it's an assignment statement (conditions are written with double equal signs) 它甚至不是条件,它是赋值语句(条件用双等号编写)
  2. Element name should be compared to chk[] instead of chk 元素名称应与chk []而不是chk进行比较

Your checkall function should be: 您的checkall函数应为:

function checkall(frm) {
  for (var i=0; i<frm.elements.length; i++) {
    if (frm.elements[i].name == "chk[]")
      frm.elements[i].checked = true;
  }
}

Now you might be wondering what the hell does this have to do with the values not being submitted, and why are they checked, I can explain. 现在您可能想知道这与未提交的值到底有什么关系,为什么可以检查它们,我可以解释一下。

Your current code is assigning a new name "chk" to the checkbox elements (see reason 1 why your condition is not even a condition), and since it's not a condition, the if statement is always true, and the code segment that sets the checked value to true is being executed and that's why you can see the elements checked. 您当前的代码为复选框元素分配了一个新名称“ chk”(请参见原因1,您的条件甚至不是条件),并且由于它不是条件,因此if语句始终为true,并且代码段设置了检查值是否为true正在执行,这就是为什么您可以看到检查的元素的原因。 Now when you request $_POST['chk[]'] in your PHP you get nothing because the elements' names were all changed to 'chk' by your supposedly (if "condition"). 现在,当您在PHP中请求$ _POST ['chk []']时,您一无所获,因为假定您的元素名称全部更改为“ chk”(如果为“ condition”)。

iSWORD has a complete answer, this is just some additional hints. iSWORD有一个完整的答案,这只是一些其他提示。 You can simplify the function by taking advantage of built–in browser features: 您可以利用内置的浏览器功能来简化功能:

function checkall(frm) {
  var cbs = frm['chk[]'];
  for (var i=0, iLen=cbs.length; i<iLen; i++) {
    cbs[i].checked = true;
  }
}  

Form controls are available as named properties of the form. 表单控件可用作表单的命名属性。 Where more than one control shares the same name, an HTML collection is returned. 如果多个控件共享相同的名称,则返回HTML集合。 So in the above, cbs is a collection of all the controls with a name of 'chk[]' . 因此,在上面, cbs是所有控件的集合,名称为'chk[]'

To account for cases where there might be zero, one or more same–named controls, use: 要考虑可能存在零个,一个或多个同名控件的情况,请使用:

function checkall(frm) {
  var cbs = frm['chk[]'];
  if (cbs) {
    if (typeof cbs.length == 'number') {
      for (var i=0, iLen=cbs.length; i<iLen; i++) {
        cbs[i].checked = true;
      }
    } else {
      cbs.checked = true;
    }
  }
}  

You could also use querySelectorAll , which always returns a collection (but might not be available everywhere) and use the following with the first function: 您还可以使用querySelectorAll ,它始终返回一个集合(但可能并非在所有地方都可用),并在第一个函数中使用以下内容:

var cbs = document.querySelectorAll('input[name="chk[]"]');

You can try using this function 您可以尝试使用此功能

Javascript Java脚本

function checkall(formname,checkname,thestate){
    var el_collection=eval("document.forms."+formname+"."+checkname)
    for (c=0;c<el_collection.length;c++)
    el_collection[c].checked=thestate
}

HTML 的HTML

<a href="javascript:checkall('test','chk',true)">Check All</a>
<a href="javascript:checkall('test','chk',false)">Uncheck All</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM