Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Jquery从复选框中提取数据_Php_Jquery_Forms - Fatal编程技术网

Php 使用Jquery从复选框中提取数据

Php 使用Jquery从复选框中提取数据,php,jquery,forms,Php,Jquery,Forms,我对Jquery和javascript还相当陌生,我已经先入为主,有点卡住了 我有一个页面,点击一个按钮可以通过一个AJAX调用打开一个小的覆盖,然后给用户一大堆复选框来选择他们想要与关键字关联的标签。然后,我尝试通过另一个ajax调用来处理复选框。到目前为止,一切看起来都很好,除了我无法获得勾选复选框和未通过表单的数据 我正在使用jquery、PHP和MySQL组合;请参阅下面的代码片段 <form method="post" > <h1>Categories</

我对Jquery和javascript还相当陌生,我已经先入为主,有点卡住了

我有一个页面,点击一个按钮可以通过一个AJAX调用打开一个小的覆盖,然后给用户一大堆复选框来选择他们想要与关键字关联的标签。然后,我尝试通过另一个ajax调用来处理复选框。到目前为止,一切看起来都很好,除了我无法获得勾选复选框和未通过表单的数据

我正在使用jquery、PHP和MySQL组合;请参阅下面的代码片段

<form method="post" >
<h1>Categories</h1> 
<fieldset id="edit_box">
        <legend>Choose Tags</legend>

<?php
// $tags is an array with all the original values from the database, so a list of available tags and if they are set for each category
foreach($tags as $value){
        if(isset($value['set']) && $value['set'] ==1) $set = "checked = 'checked'"; else $set = "";
        echo'<p class="floatlist">
        <input type="checkbox" name="'.$value['id'].'" id="'.$value['id'].'" '.$set.' />
        <label for="'.$value['id'].'" class="inline">'.$value['name'].'</label>
</p>';
}
        ?>
<p><button type="button" id="update_tags">Save Changes</button></p>
</fieldset>     
</form> 

<script type="text/javascript">
        $("#update_tags").click(function(){
                var data = new Array();
                $('#edit_box input').each(function() {
                        var tagid = $(this).attr('id');
                        if($('#'+tagid ).attr('checked')){data[tagid] = '1';} else {data[tagid] = 0;}
                });

                $.post("inc/process_tags.php", { page: "update_tags", categories: data }, function(data){
                        var output = data;
                        $('#edit_box').html(output);

                });                             
        });

</script>
目前,我从中得到一个与原始选项相对应的数组,因此,如果项目13和5被选中,它们将正确显示。但是,如果我在提交之前更改变量,更改它,以便项目12和5被选中,则仍将输出显示为被选中的原始项目13和5。我错过了什么

不幸的是,我没有机会给你一个活生生的例子


提前谢谢

Jquery作为可以选择选中元素的功能,适用于单选和复选框。查看选中的选择器

有两种选择:

如果您需要JS中选定的复选框,假设这些复选框共享一个公共类:

$(".checkbox_class:checked").each(function(){
   // All of the checked boxes are iterated, $(this).val();
});
如果您希望在PHP中的$\u POST对象中列出所选复选框,则应首先更改标记,使每个复选框都有一个通用名称数组:

<input type="checkbox" name="tags[]" value="php the value" />

如果您只需要检查项目:

  $("#update_tags").bind("click", function(){
    var data = [];

    // :visible is optional
    $('#edit_box').find("input[type='checkbox']:checked:visible").each(function(){
      var $t = $(this),           // current checkbox
          id = $t.attr('id');     // checkbox id

      data[id] = '1';

      // insert additional code here

   });
  });
或者,如果要遍历所有复选框元素:

  $("#update_tags").bind("click", function(){
    var data = [];

    // :visible is optional
    $('#edit_box').find("input[type='checkbox']:visible").each(function(){
      var $t = $(this),           // current checkbox
          id = $t.attr('id');     // checkbox id

      data[id] = $t.is(":checked") ? '1' : '0';

     // insert additional code here

   });
  });

希望这能有所帮助

我不确定我是否理解这个问题。在我提交之前更改变量是什么意思?javascript post submit代码收集所有当前输入值并将它们发布到inc/process_tags.php,只要单击Save Changes按钮,该代码就会运行。此时,它将提交表单的当前值。如果以后更改了选择,则需要再次保存更改。这几乎完成了!我现在只输出一个打印来测试,过了一秒钟,它就删除了列表中的最后一项?知道为什么会这样吗?谢谢嗯,我做了件蠢事。。。它工作得很好!再次感谢
  $("#update_tags").bind("click", function(){
    var data = [];

    // :visible is optional
    $('#edit_box').find("input[type='checkbox']:visible").each(function(){
      var $t = $(this),           // current checkbox
          id = $t.attr('id');     // checkbox id

      data[id] = $t.is(":checked") ? '1' : '0';

     // insert additional code here

   });
  });