ahm*_*med 26 html javascript ajax jquery
如何使用jQuery ajax post传递多个复选框
这是ajax功能
function submit_form(){
$.post("ajax.php", {
selectedcheckboxes:user_ids,
confirm:"true"
},
function(data){
$("#lightbox").html(data);
});
}
Run Code Online (Sandbox Code Playgroud)
这是我的表格
<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>
Run Code Online (Sandbox Code Playgroud)
Pau*_*jan 48
从POST的jquery文档(第3个例子):
$.post("test.php", { 'choices[]': ["Jon", "Susan"] });
Run Code Online (Sandbox Code Playgroud)
所以我只是遍历复选框并构建数组.就像是
var data = { 'user_ids[]' : []};
$(":checked").each(function() {
data['user_ids[]'].push($(this).val());
});
$.post("ajax.php", data);
Run Code Online (Sandbox Code Playgroud)
sim*_*nom 16
刚刚遇到这个尝试找到同样问题的解决方案.实现Paul的解决方案我做了一些调整以使这个功能正常.
var data = { 'venue[]' : []};
$("input:checked").each(function() {
data['venue[]'].push($(this).val());
});
Run Code Online (Sandbox Code Playgroud)
简而言之,添加输入:checked而不是:checked将输入到数组中的字段限制为仅在表单上的复选框.保罗确实是正确的,this
需要被封闭$(this)
Phi*_*son 10
可以使用以下内容然后展开post结果explode(",", $_POST['data']);
以提供一系列结果.
var data = new Array();
$("input[name='checkBoxesName']:checked").each(function(i) {
data.push($(this).val());
});
Run Code Online (Sandbox Code Playgroud)
小智 6
这是一种更灵活的方式.
让我们说这是你的形式.
<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>
Run Code Online (Sandbox Code Playgroud)
这是你下面的jquery ajax ......
// Don't get confused at this portion right here
// cuz "var data" will get all the values that the form
// has submitted in the $_POST. It doesn't matter if you
// try to pass a text or password or select form element.
// Remember that the "form" is not a name attribute
// of the form, but the "form element" itself that submitted
// the current post method
var data = $("form").serialize();
$.ajax({
url: "link/of/your/ajax.php", // link of your "whatever" php
type: "POST",
async: true,
cache: false,
data: data, // all data will be passed here
success: function(data){
alert(data) // The data that is echoed from the ajax.php
}
});
Run Code Online (Sandbox Code Playgroud)
在你的ajax.php中,你试着回复或print_r你的帖子,看看里面发生了什么.这应该是这样的.仅返回您选中的复选框.如果您没有选中任何内容,则会返回错误.
<?php
print_r($_POST); // this will be echoed back to you upon success.
echo "This one too, will be echoed back to you";
Run Code Online (Sandbox Code Playgroud)
希望足够清楚.
归档时间: |
|
查看次数: |
98695 次 |
最近记录: |