php - 在 php 的 javascript 中获取 Checkbox[] 值

标签 php javascript checkboxlist

我在获取复选框中的值时遇到问题。我试图清理代码。

我已经能够选中所有选中的复选框(不是每行),或者我可以获得每行的复选框 ID 但不是选中的值。我很确定我可以将这些值转移到另一个页面,但不知道如何获取它们。

如有任何帮助,我们将不胜感激!

 //php variables used
 $pages = 2
 $size = 4 
  <form id="form" name="cb">
  <div style=" width:800px; height:500px; overflow:auto">
  <h2>Select Editions</h2>
  <table id='table' name='table' border=1 cellpadding=7 width=50% height=50% align='center'>\n
    for($x = 1; $x <= $pages; $x++) :
      print "<td id='page_$x' class='page_button' align='center' custom='0' >Page $x - ";
        for($i = 1; $i <= $size; $i++) :
          print "<input type='checkbox' class='ebutton' id='etype_$x' name='checks[]' value='$i' /> $i";
        endfor;
      </td>
      </tr>
    //php end for loop
    endfor;
  </div>
  </form>

这是基于复选框类的类更新的 Javascript

    $(document).ready(function() {
      $(".ebutton").change(function() {

        var idp = $(this).attr("id").split("_");
        var page_num = idp[1];

        //  I need to find out how to get the checkboxes that are checked per row. Ex: 01,02
        //var editions = ?;
        //alert(editions);

        var hidden_id = "#etype_page_" + page_num;
          if($(hidden_id).length < 1) {
            $("#base").append('<input type="hidden" id="etype_page_'+ page_num +'" name="'+ page_num +'"  value="'+ editions +'" class="hidden_edtype" custom="' + editions +'">');
          } else {
            $(hidden_id).val($(this).val());
          }
        update_eShow();
      });
    });

    function update_eShow() {
      $("#eShow").html('');
        $(".hidden_edtype").each(function() {
         var page = $(this).attr("name");
         var value = $(this).attr("custom");
         $("#eShow").append('page:' + page + ' values:' + value +'<br>');
      });
    }

页面看起来像这样:

|第 1 页 - []1 []2 []3 []4 |

|第 2 页 - []1 []2 []3 []4 |

最佳答案

姓名应该有不同的号码,以表格形式发送的姓名应该是唯一的,就像您的 ID 一样。

尝试像 id 一样设置名称:name='etype_$x'

然后使用调试控制台(如果使用 chrome 按 f12)然后转到网络并可视化在表单中发送的变量和值。

关于php - 在 php 的 javascript 中获取 Checkbox[] 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13713227/

相关文章:

php - 在一个 process.php 文件中提交第二个查询之前,如何查询 SQL 数据库?

php - Codeigniter SMTP 无法连接

javascript - jQuery 延迟或 JavaScript SetTimeOut 简单解决方案

c# - 在 asp.net CheckBoxList 中选择所有项目

android - 使用复选框和搜索实现处理 ListView

php - php/mysql 中未定义索引的问题

php - 这个多重可选过滤器的最佳解决方案是什么?

javascript - React + Redux - 等待操作结束?

javascript - Ajax 请求数据无法显示到输入字段中

asp.net - 如何验证用户在 CheckBoxList 中至少选择了一个复选框?