简体   繁体   English

脚本在表中每一行的单元格中均不起作用

[英]Script not working in cell in every row in table

I have a row/column table with switches, unfortunately the script can't find 我有一个带有开关的行/列表,不幸的是脚本找不到

the switches in the new column, basically after the end of the cell . 基本上在单元格结束之后,在新列中进行切换。

I tried modifying some of the scripts that were posted in the similar posts but none of them worked. 我尝试修改过类似帖子中发布的一些脚本,但是它们都不起作用。 I am pretty much stuck at this point. 在这一点上,我几乎陷入了困境。

Here's my HTML and the java script that does the calculation for the switches: 这是我的HTML和Java脚本,用于计算开关:

    <div style="margin-left:-178px">
      <form action="file:///Users/jason/Desktop/Temp/JACK010.txt" target="_blank">
        <font color="white">JACK010</font>
        <input type="submit" value="CHECK ADDRESS">
          <div id="checkbox-container">
            <div style="margin-left:178px">
              <div style="margin-top:-18px">
                <div>
                  <input type="checkbox" id="option170">
                </div>
      </form>
  </td>
</tr>

I have a lot of those, within the cell, and finally the JavaScript: 我在单元格中有很多,最后还有JavaScript:

var checkboxValues = JSON.parse(localStorage.getItem('checkboxValues')) || {};
var $checkboxes = $("#checkbox-container :checkbox");
$checkboxes.on("change", function() {
  $checkboxes.each(function() {
    checkboxValues[this.id] = this.checked;
  });
  localStorage.setItem("checkboxValues", JSON.stringify(checkboxValues));
});
$.each(checkboxValues, function(key, value) {
  $("#" + key).prop('checked', value);
});

Thank you! 谢谢!

Using only $("#checkbox-container :checkbox"); 仅使用$("#checkbox-container :checkbox"); results in only reading the first container with the ID checkbox-container . 导致仅读取ID为checkbox-container的第一个checkbox-container

If you want to fetch all checkboxes, you should either change the checkbox-container to a class or use the following selector within jQuery: 如果要获取所有复选框,则应将checkbox-container更改为一个类,或在jQuery中使用以下选择器:

var $checkboxes = $("div#checkbox-container :checkbox");

It will then loop through all the checkboxes, even if an ID is used instead of a class. 然后,即使使用ID代替类,它也会循环遍历所有复选框。

Keep in mind, that the ID of your checkbox ( <input type="checkbox" id="option170"> ) must however change in order to get valid data and all the values you want. 请记住,复选框的ID( <input type="checkbox" id="option170"> )必须更改,以便获取有效数据和所需的所有值。

Side note: Please keep your HTML code clean and valid. 旁注:请保持您的HTML代码干净有效。

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

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