dsceme82487 2016-08-25 00:51
浏览 66
已采纳

在数组中使用未选中和选中的复选框

I have a table that allows the user to add rows to it. The table has four columns. Items, Qty, Price, and Tax. The tax column is a check box. I am using the hidden input to set a value for an unchecked box. This way I can take the post data and know if I should charge tax on a specific row. (0 value means no tax, 1 value means charge tax) The problem I have is when putting check boxes in an array it will always pick up the hidden value.

Table:

<table style="width: 90%" id="myTable" class="centered-table table table-bordered">
 <thead>
  <tr>
     <th>Item*</th>
     <th>Qty*</th>
     <th>Price*</th>
     <th>Tax</th>
     <th>Action</th>
    </tr>
   </thead>
   <tbody>
    <tr>
       <td style="width: 60%"><input type="text" id="detail" name="detail[]"required></td>
       <td style="width: 8%"><input type="number" id="qty" name="qty[]" required></td>
       <td style="width: 12%"><input type="number" id="price" name="price[]" required></td>
       <input type="hidden" value="0" name="tax[]">
       <td style="width: 5%"><input type="checkbox" id="tax" name="tax[]" value="1"></td>
       <td style="width: 12%"><div class="inline"><input type="button" id="addButton" class="btn btn-primary btn-xs" value="Add"/></div><div class="inline"><input type="button" id="deleteButton" class="btn btn-primary btn-xs" value="Delete"/></div>
      </tr>
    </tbody>

For example: I have four rows. The first and last row are checked. The array looks like this [0,1,0,0,0,1] I need the array to look like this [1,0,0,1] Is this possible or do I need to try a completely different method of getting unchecked and checked boxes? I have tried to come up with a loop that will take away zeros but I can always find a combination of checked and unchecked boxes that will make it fail.

This is my javascript to add rows to the table

$(function(){
    $("#addButton").click(function(){
        $(this).closest("tr").clone(true).appendTo("#myTable");
    });

$("#deleteButton").click(function(){
    var x = $('#myTable tr').length;
    if(x == 2){

    } else {
         $(this).closest("tr").remove();
    }

    });
});
  • 写回答

1条回答 默认 最新

  • duanchuan6350 2016-08-25 01:04
    关注

    Unchecked checkboxes are simply never sent to server, and you need to adjust your form to accommodate that. I would suggest something like:

    <form ...>
        <input name="obj[0][name]" type="text">
        <input name="obj[0][qty]" type="text">
        <input name="obj[0][price]" type="text">
        <input name="obj[0][tax]" type="checkbox">
    
        <input name="obj[1][name]" type="text">
        <input name="obj[1][qty]" type="text">
        <input name="obj[1][price]" type="text">
        <input name="obj[1][tax]" type="checkbox">
        ...
    </form>
    

    And then you can reliably test for whether or not that a given box was checked by checking for the existence of the tax index in the resulting array which should look something like:

    $_POST['obj'] == [
        [
            'name' => ...,
            'qty' => ...,
            'price' => ...
        ],
        [
            'name' => ...,
            'qty' => ...,
            'price' => ...,
            'tax' => ...
        ]
    ]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?