gpt4 book ai didi

jquery - 将复选框数组格式化为 csv

转载 作者:行者123 更新时间:2023-12-01 06:29:27 25 4
gpt4 key购买 nike

我有多个带有 name=ap[] 的复选框,并且想要使用 jquery 将选中的复选框值转换为逗号分隔值,以便我可以解析为查询字符串。我该怎么办? .serialize() 使字符串变得过于复杂

最佳答案

jQuery.fn.map迭代 jQuery 集,并返回一个新数组(在 jQuery 包装器内),其中包含等于每个迭代的返回值的节点。然后,您可以通过调用 jQuery.fn.get 来获取该数组。 ,然后将该数组用于您想要的任何内容:

$(':checkbox:checked').map(function(){
return $(this).val(); // value will be in the returned array
})
.get() // extract the new array from the jQuery object
.join(','); //do anything you want with the array

在您的情况下,可以这样应用:

var list = $(':checkbox[name="ap[]"]:checked').map(function(){
return $(this).val();
}).get().join(',');

alert(list);

关于jquery - 将复选框数组格式化为 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8984732/

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