dongmaopan5738 2014-02-24 17:33
浏览 74
已采纳

强制发布复选框数组输入/解决方法

Sorry for the bad title.. I am trying to figure out a workaround to this fairly simple problem I am having.

HTML

<input type="text" name="test[description][]" / >
<input type="checkbox" name="test[confirm][]" value="1" / >

The html can be cloned by jquery..the problem I am having is that the checkbox wont post unless it's checked (this is what it's supposed to do I know). This messes up my PHP to check the checkbox index to see if it's checked.. if you clone this 3 times then leave the middle checkbox unchecked..but check the first and last.. the last checkbox gets index of 2 when it want it to be 3.

any suggestions are appreciated thank you.

  • 写回答

1条回答 默认 最新

  • dongshedan4672 2014-02-24 17:39
    关注

    I don't think you can do it the way you're trying to do it. There's no way to differentiate which checkboxes were checked as written. You can either:

    1) Encode the position in the element name.

    2) Create hidden fields, which will be sent, to record the checked/unchecked value. Initialize with something like:

    <input id="hidden-1" type="hidden" name="test[description][]" value="0" />
    <input id="check-1" type="checkbox" />
    <script>
       $('#check-1').click(function() {
          var $hidden = $('#hidden-1');
          $hidden.val(hidden.val() == 0 ? 1 : 0);
       }
    </script>
    

    If the number is dynamic, you could encapsulate all that to generate the bindings dynamically as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器