dqyuipw44576 2011-09-08 10:36
浏览 82
已采纳

如何使用JQuery通过_POST将所有复选框的数组传递到下一页

How to get an array of all checkboxes selected on the page, and then passing it to the next page (in this case php, so it can be picked up by php _POST function). I have come up with this :

<script type="text/javascript">
var selected = new Array();

$(document).ready(function() {

  $("input:checkbox:checked").each(function() {
       selected.push($(this).val());
  });
$('#od').submit(function() {
  alert(this.selected); // *See note below
  $.post('receiver.php', {'registration': selected});
  return false;
});

});

</script>

But is does not seem to work :( It returns null, as if no checkboxes would have been added to the array, or maybe the post function is wrong. Can you point me in the right direction here?

  • 写回答

4条回答 默认 最新

  • dr6673999 2011-09-08 11:01
    关注

    I've found the following of issues:

    1. You read the checked items on page load, thus ignoring all changes made by the user. Move that code to the submit() handler.

    2. Your debugging code (alert(this.selected)) tries to display the value of the selected property for the form node. It isn't a reference to your JavaScript global variable selected. I suggest you use a proper debugging tool such as Firebug; alerts are highly unsuitable.

    3. You sucessfully send the values of checked items. You just ignore the field name and rename everything to registration. That looks intended, otherwise report back:

      {'registration': selected}
      

    I suppose you can make jQuery serialize the values for you but fixing these little details in your code should do the trick anyway.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥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()实现黑框里写入与删除?