如何在jquery中获取所选复选框的值并将其分配给json数组?

时间:2014-04-28 05:02:36

标签: javascript php jquery json

我有一个问题,我对jquery一无所知,这就是为什么我的代码很难。我有一个产品的分页。它上面有复选框。我的简单目标是在选中复选框后禁用产品。但我不能用一种形式来做,因为我已经在我的表单中有另一个进程,如果选择了删除产品。这就是为什么我打算在我的jquery中创建一个表单操作,并将我的复选框中的所有产品ID传递给json数组。但是我该怎么做呢?

这是我的一些代码

<form action="<?php echo $delete; ?>" method="post" enctype="multipart/form-data" id="form">
  <table class="list">
    <thead>
      <tr>
        <td width="1" style="text-align: center;"><input type="checkbox" onclick="$('input[name*=\'selected\']').attr('checked', this.checked);" /></td>
        <td class="left"><?php echo $column_name; ?></td>
        <td class="right"><?php echo $column_sort_order; ?></td>
        <td class="left"><?php echo $column_status; ?></td>
        <td class="right"><?php echo $column_action; ?></td>
      </tr>
    </thead>
    <tbody>
      <?php if ($categories) { ?>
      <?php foreach ($categories as $category) { ?>
      <tr>
        <td style="text-align: center;"><?php if ($category['selected']) { ?>
          <input type="checkbox" name="selected[]" value="<?php echo $category['category_id']; ?>" checked="checked" />
          <?php } else { ?>
          <input type="checkbox" name="selected[]" value="<?php echo $category['category_id']; ?>" />
          <?php } ?></td>
        <td class="left"><?php echo $category['name']; ?></td>
        <td class="right"><?php echo $category['sort_order']; ?></td>
        <td class="right"><?php echo ($category['status'] == 1 ? 'Enable' : 'Disable'); ?></td>
        <td class="right"><?php foreach ($category['action'] as $action) { ?>
          [ <a href="<?php echo $action['href']; ?>"><?php echo $action['text']; ?></a> ]
          <?php } ?></td>
      </tr>
      <?php } ?>
      <?php } else { ?>
      <tr>
        <td class="center" colspan="4"><?php echo $text_no_results; ?></td>
      </tr>
      <?php } ?>
    </tbody>
  </table>
</form>

<br />
<div align="right">
  <select name="action_select">
    <option value="enable">Enable Selected</option>
    <option value="disable">Disable Selected</option>
  </select>
  <input type="button" class="button" id="update_status" value="Update Status" />
</div>
<br />

这是我的jquery示例

<script type="text/javascript">

    $("#update_status").on('click', function(){

        //how can I get the id of my checkboxes and assign it to a json array
        //How can I create a form inside it?

    });

</script>

这是所有人,我希望你能理解我的问题。感谢。

3 个答案:

答案 0 :(得分:1)

$('input[name="selected[]"]:checked').each(function() {
   console.log(this.value);
});

了解更多信息: - use jQuery to get values of selected checkboxes

答案 1 :(得分:1)

否则,您可以使用以下示例代码
<script> $(document).ready(function() { $("input[type=checkbox]").click(function() { var categoryVals = []; categoryVals.push(''); $('#Category_category :checked').each(function() { categoryVals.push($(this).val()); }); $.ajax({ type:"POST", url:"<?php echo $this->createUrl('ads/searchresult'); ?>", //url of the action page data:{'category': categoryVals}, success : function(response){ //code to do somethng if its success } }); } } </script>

答案 2 :(得分:0)

尝试

var values = $('.list input[name="selected[]"]:checked').map(function () {
    return this.value;
}).get();