gpt4 book ai didi

javascript - jquery 复选框和数组帮助

转载 作者:行者123 更新时间:2023-11-30 13:40:26 25 4
gpt4 key购买 nike

您好,我需要将已选中的复选框的名称和值获取到选定的数组名称中,我终生无法正常工作,下面是我的尝试,如果有人可以澄清我做错了什么会很棒。

//Location AJAX
//var dataObject = new Object();
var selected = new Array();
$('#areas input.radio').change(function(){ // will trigger when the checked status changes
var checked = $(this).attr("checked"); // will return "checked" or false I think.
// Do whatever request you like with the checked status
if(checked == true) {
/*$("input:checked").each(function() {
selected.push($(this).attr('name')+"="+$(this).val();
});
alert(selected)*/
getQuery = $(this).attr('name')+"="+$(this).val()+"&location_submit=Next";
$.ajax({
type:"POST",
url:"/search/location",
data: getQuery,
success:function(data){
alert(getQuery);
console.log(data);
// $('body.secEmp').html(data);
}
});
} else {
//do something to remove the content here
alert("Remove");
}
});

最佳答案

您在调用 selected.push 时缺少结束括号。应该是:

selected.push($(this).attr('name')+"="+$(this).val());

否则,它看起来很好。

另一种可能更简单的方法是使用 map 而不是 each:

var selected = $('input:checked').map(function() {
return $(this).attr('name')+"="+$(this).val();
}).get();

关于javascript - jquery 复选框和数组帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2622738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com