jQuery 从ajax中选中的复选框中获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19131800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:15:21 来源:igfitidea点击:
getting value from checked checkbox in ajax
提问by Krishna
I want to get the values of the checked checkbox here but it is undefined because it becomes an array.. Please help me how to do it.. thank you..
我想在这里获取选中复选框的值,但它未定义,因为它变成了一个数组..请帮我怎么做..谢谢..
<form>
<input type="checkbox" value="a" name="letter[]" />
<input type="checkbox" value="b" name="letter[]" />
<input type="checkbox" value="c" name="letter[]" />
<input type="button" value="submit" onclick="rani()" />
</form>
<script>
function rani(){
$.ajax({
type:"post",
url:"ajax.php",
data:{
radha:$("[name=letter]").val(),
},
success:function(msg){
alert(msg);
}
})
}
</script>
回答by Satinder singh
Here you will get selected checkbox values in a variable checkbox_value
and later split by |
.
在这里,您将在变量中获得选定的复选框值,checkbox_value
然后按|
.
$("#btn_").on('click', function () {
var checkbox_value = "";
$(":checkbox").each(function () {
var ischecked = $(this).is(":checked");
if (ischecked) {
checkbox_value += $(this).val() + "|";
}
});
alert(checkbox_value);
// your awesome code calling ajax
});
回答by Arun P Johny
Try
尝试
function rani() {
var letters = $('input[name="letter[]"]:checked').map(function(){
return this.value;
}).get()
$.ajax({
type: "post",
url: "ajax.php",
data: {
radha: letters,
},
success: function (msg) {
alert(msg);
}
})
}
otherwise try
否则试试
function rani() {
$.ajax({
type: "post",
url: "ajax.php",
data: $('form').serialize(),
success: function (msg) {
alert(msg);
}
})
}