如何通过使用jQuery阿贾克斯后多个复选框 [英] How to pass multiple checkboxes using jQuery ajax post

查看:134
本文介绍了如何通过使用jQuery阿贾克斯后多个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过使用jQuery阿贾克斯后多个复选框

这是Ajax的功能

 函数submit_form(){
 $。员额(ajax.php,{
 selectedcheckboxes:user_ids,
 确认:真正的
 },
 功能(数据){
  $(#收藏夹)HTML(数据);
  });
 }
 

这是我的表格

 <形式GT;
<输入类型=复选框名称='user_ids []'值='1'id ='checkbox_1'/>
<输入类型=复选框名称='user_ids []'值='2'id ='checkbox_2'/>
<输入类型=复选框名称='user_ids []'值='3'id ='checkbox_3'/>
<输入名称=确定类型=按钮值=确认的onclick =submit_form(); />
< /形式GT;
 

解决方案

从jQuery文档的POST:

  $后(test.php的,{'选择[]:夫君,苏珊]})。
 

所以,我只想遍历检查框和生成数组。类似于

  VAR数据= {'user_ids []:[]};
$(:选中)每个(函数(){。
  。数据['user_ids []']推($(本).VAL());
});
$ .post的(ajax.php,数据);
 

How to pass multiple checkboxes using jQuery ajax post

this is the ajax function

 function submit_form(){
 $.post("ajax.php", {
 selectedcheckboxes:user_ids,
 confirm:"true"
 },
 function(data){
  $("#lightbox").html(data);
  });
 }

and this is my form

<form>
<input type='checkbox' name='user_ids[]' value='1'id='checkbox_1' />
<input type='checkbox' name='user_ids[]' value='2'id='checkbox_2' />
<input type='checkbox' name='user_ids[]' value='3'id='checkbox_3' />
<input name="confirm" type="button" value="confirm" onclick="submit_form();" />
</form>

解决方案

From the jquery docs for POST :

$.post("test.php", { 'choices[]': ["Jon", "Susan"] });

So I would just iterate over the checked boxes and build the array. Something like

var data = { 'user_ids[]' : []};
$(":checked").each(function() {
  data['user_ids[]'].push($(this).val());
});
$.post("ajax.php", data);

这篇关于如何通过使用jQuery阿贾克斯后多个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆