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 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler