Javascript 获取数组中所有选定的复选框
所以我有这些复选框:Javascript 获取数组中所有选定的复选框,javascript,jquery,ajax,dhtml,Javascript,Jquery,Ajax,Dhtml,所以我有这些复选框: <input type="checkbox" name="type" value="4" /> <input type="checkbox" name="type" value="3" /> <input type="checkbox" name="type" value="1" /> <input type="checkbox" name="type" value="5" /> 等等。其中大约有6个是手工编码的(即不是
<input type="checkbox" name="type" value="4" />
<input type="checkbox" name="type" value="3" />
<input type="checkbox" name="type" value="1" />
<input type="checkbox" name="type" value="5" />
等等。其中大约有6个是手工编码的(即不是从数据库中获取的),因此它们可能会保持不变一段时间
我的问题是如何将它们全部放在一个数组中(在javascript中),以便在使用Jquery发出AJAX$请求时使用它们
有什么想法吗
编辑:我只想将所选复选框添加到数组中格式:
$("input:checkbox[name=type]:checked").each(function(){
yourArray.push($(this).val());
});
希望它能起作用。这应该可以做到:
$('input:checked');
我认为你没有其他可以检查的元素,但如果你检查了,你必须让它更具体:
$('input:checkbox:checked');
$('input:checkbox').filter(':checked');
我没有测试它,但它应该工作
<script type="text/javascript">
var selected = new Array();
$(document).ready(function() {
$("input:checkbox[name=type]:checked").each(function() {
selected.push($(this).val());
});
});
</script>
所选变量=新数组();
$(文档).ready(函数(){
$(“输入:复选框[名称=类型]:选中”)。每个(函数(){
已选择.push($(this.val());
});
});
在MooTools 1.3中(最迟在撰写本文时):
var chk_arr=document.getElementsByName(“chkRights[]);
var chklength=chk_arr.长度;
对于(k=0;k
在Javascript中,它是这样的:
//获取所选复选框
功能getSelectedChbox(frm){
var selchbox=[];//将存储选定复选框值的数组
//获取frm中的所有输入标记及其编号
var inpfields=frm.getElementsByTagName('input');
var nr_inpfields=inpfields.length;
//遍历inpfields元素,并在selchbox中添加selected(checked)复选框的值
对于(var i=0;i使用Jquery
您只需要添加类到每个输入,我已经添加类“源”,您当然可以更改它
<input class="source" type="checkbox" name="type" value="4" />
<input class="source" type="checkbox" name="type" value="3" />
<input class="source" type="checkbox" name="type" value="1" />
<input class="source" type="checkbox" name="type" value="5" />
<script type="text/javascript">
$(document).ready(function() {
var selected_value = []; // initialize empty array
$(".source:checked").each(function(){
selected_value.push($(this).val());
});
console.log(selected_value); //Press F12 to see all selected values
});
</script>
$(文档).ready(函数(){
var selected_value=[];//初始化空数组
$(“.source:选中”).each(函数(){
所选的_value.push($(this.val());
});
console.log(选定的_值);//按F12键查看所有选定的值
});
如果您想使用vanilla JS,可以使用与@zahid ullah类似的方法,但要避免循环:
var values = [].filter.call(document.getElementsByName('fruits[]'), function(c) {
return c.checked;
}).map(function(c) {
return c.value;
});
ES6中的相同代码看起来更好:
var values = [].filter.call(document.getElementsByName('fruits[]'), (c) => c.checked).map(c => c.value);
window.serialize=函数serialize(){
var values=[].filter.call(document.getElementsByName('fruits[]),函数(c){
返回c.checked;
}).map(功能(c){
返回c.value;
});
document.getElementById('serialized').innerText=JSON.stringify(值);
}
标签{
显示:块;
}
香蕉
苹果
桃
橙色
草莓的
连载
使用以下方法:
var arr = $('input:checkbox:checked').map(function () {
return this.value;
}).get();
ES6版本:
const values = Array
.from(document.querySelectorAll('input[type="checkbox"]'))
.filter((checkbox) => checkbox.checked)
.map((checkbox) => checkbox.value);
函数getCheckedValues(){
返回数组.from(document.querySelectorAll('input[type=“checkbox”]”)
.filter((复选框)=>checkbox.checked)
.map((复选框)=>checkbox.value);
}
const resultEl=document.getElementById('result');
document.getElementById('showResult')。addEventListener('click',()=>{
resultEl.innerHTML=getCheckedValues();
});
1
2.
3.
4.
5.
显示选中的值
纯JS
对于那些不想使用jQuery的人
var array = []
var checkboxes = document.querySelectorAll('input[type=checkbox]:checked')
for (var i = 0; i < checkboxes.length; i++) {
array.push(checkboxes[i].value)
}
var数组=[]
var checkbox=document.querySelectorAll('input[type=checkbox]:checked')
对于(变量i=0;i
如果使用按钮单击或其他方法运行插入,请使用注释的if块来防止添加数组中已有的值
$('#myDiv')。更改(函数(){
var值=[];
{
$('#myDiv:checked')。每个(函数(){
//if(values.indexOf($(this.val())=-1){
value.push($(this.val());
// }
});
console.log(值);
}
});
选中时为复选框添加值,取消选中时减去值
$('#myDiv')。更改(函数(){
var值=0.00;
{
$('#myDiv:checked')。每个(函数(){
//if(values.indexOf($(this.val())=-1){
values=values+parseFloat($(this.val());
// }
});
log(parseFloat(value));
}
});
您可以尝试以下方法:
$('input[type="checkbox"]').change(function(){
var checkedValue = $('input:checkbox:checked').map(function(){
return this.value;
}).get();
alert(checkedValue); //display selected checkbox value
})
这里
这是我的代码,有人也可以尝试这个问题。
jquery
<script>
$(document).ready(function(){`
$(".check11").change(function(){
var favorite1 = [];
$.each($("input[name='check1']:checked"), function(){
favorite1.push($(this).val());
document.getElementById("countch1").innerHTML=favorite1;
});
});
});
</script>
$(文档).ready(函数(){`
$(“.check11”).change(函数(){
var favorite1=[];
$.each($($(输入[name='check1']:选中)),函数(){
favorite1.push($(this.val());
document.getElementById(“countch1”).innerHTML=favorite1;
});
});
});
函数选择值(ele){
var-arr=[];
对于(变量i=0;i }
在现代浏览器中使用vanilla JS的另一种方法(不支持IE,遗憾的是在编写本文时不支持iOS Safari)是:
纯JavaScript,不需要临时变量:
Array.from(document.querySelectorAll("input[type=checkbox][name=type]:checked")).map(e => e.value)
按输入名称选择复选框
var category_id = [];
$.each($("input[name='yourClass[]']:checked"), function(){
category_id.push($(this).val());
});
可以使用我创建的这个函数
function getCheckBoxArrayValue(nameInput){
let valores = [];
let checked = document.querySelectorAll('input[name="'+nameInput+'"]:checked');
checked.forEach(input => {
let valor = input?.defaultValue || input?.value;
valores.push(valor);
});
return(valores);
}
要使用它,就这么说吧
getCheckBoxArrayValue("type");
没有意识到问题的年龄或解决方案的性质是相关的。因为我在使用MooTools查找同一问题的答案时发现了这个问题。请注意,这个答案与MooTools 1.3有关,而不是与jQuery有关。如果取消选中复选框,将如何从中删除值array@JubinPatel您只需在执行此代码之前重置阵列。yourArray=[]
您也可以使用map
而不是each
:var-yourArray=$(“输入:复选框[name=type]:选中”).map(函数(){return$(this).val()).get()
函数get\u-selected\u-checkbox\u-array(){var-chu-list=array();$(“输入:复选框[type=checkbox]:选中”).each(函数(){chu list.push($(this.val());});返回chu list;}
push以添加v
<script>
$(document).ready(function(){`
$(".check11").change(function(){
var favorite1 = [];
$.each($("input[name='check1']:checked"), function(){
favorite1.push($(this).val());
document.getElementById("countch1").innerHTML=favorite1;
});
});
});
</script>
var formdata = new FormData(document.getElementById("myform"));
var allchecked = formdata.getAll("type"); // "type" is the input name in the question
// allchecked is ["1","3","4","5"] -- if indeed all are checked
Array.from(document.querySelectorAll("input[type=checkbox][name=type]:checked")).map(e => e.value)
var array = []
$("input:checkbox[name=type]:checked").each(function(){
array.push($(this).val());
});
var category_id = [];
$.each($("input[name='yourClass[]']:checked"), function(){
category_id.push($(this).val());
});
function getCheckBoxArrayValue(nameInput){
let valores = [];
let checked = document.querySelectorAll('input[name="'+nameInput+'"]:checked');
checked.forEach(input => {
let valor = input?.defaultValue || input?.value;
valores.push(valor);
});
return(valores);
}
getCheckBoxArrayValue("type");