[英]Javascript function that return an array of values
this function read the current selected values from a table, I can't get the list of them in a single array readable outside this function. 该函数从表中读取当前选择的值,因此无法在该函数之外可读的单个数组中获得它们的列表。
function getcheckboxed() {
vartoberead = [];
$("input:checkbox[name=test]:checked").each(function(){
vartoberead [vartoberead .length] = $(this).val();
console.log($(this).val(), vartoberead);
return vartoberead;
});
I've declared vartoberead , both inside, than outside the function, tried with: 我已经在函数内部而不是外部声明了vartoberead ,尝试了:
var myoutsidevar = getcheckboxed();
but always return 但总是回来
[undefined]
[未定义]
Every help is appreciated 每一个帮助都值得赞赏
You can shorten this up a bit: 您可以将其缩短一点:
var values = $("input:checkbox[name=test]:checked").map(function() {
return this.value;
}).get();
Edit: To make this a function and return: 编辑:使其成为一个函数并返回:
function getcheckboxed() {
return $("input:checkbox[name=test]:checked").map(function() {
return this.value;
}).get();
}
You can use map: 您可以使用地图:
var vartoberead = $("input:checkbox[name=test]:checked").map(function(){
return $(this).val();
});
map()
does actually return something, so you don't need to push to an array but instead just let jQuery fill the array for you with the returned values. map()
实际上确实返回了一些内容,因此您无需推送到数组,而只需让jQuery用返回的值为您填充数组。
you should return
after the each()
cycle (and not inside the cycle as you're doing) anyway you could also use serializeArray 无论如何,您都应该在
each()
循环之后return
(而不是在循环内部),也可以使用serializeArray
function getcheckboxed() {
return $("input:checkbox[name=test]:checked").serializeArray();
};
When you print the value inside de each ( console.log($(this).val(), vartoberead); ) it show the values correctly ? 当您在每个属性(console.log($(this).val(),vartoberead);)中打印值时,它可以正确显示值吗?
Other thing is that you are returning the value inside the each. 另一件事是您正在返回每个值内部的值。
Try this: 尝试这个:
function getcheckboxed() {
vartoberead = [];
$("input:checkbox[name=test]:checked").each(function(){
vartoberead.push($(this).val());
});
return vartoberead;
}
I have a pure javascript solution for this . 我对此有一个纯JavaScript解决方案。 I have used html array ie all checkboxes having same name (say test[])
我使用了html数组,即所有具有相同名称的复选框(例如test [])
function getCheckBoxesValues() { var values=[]; for(var i=0;i<document.getElementsByName('test[]').length;i++) { if((document.getElementsByName('test[]')[i]).checked) { values.push((document.getElementsByName('test[]')[i]).value); } } return values; }
<input type="checkbox" name="test[]" value="c1"/> <input type="checkbox" name="test[]" value="c2"/> <input type="checkbox" name="test[]" value="c3"/> <input type="checkbox" name="test[]" value="c4"/> <input type="checkbox" name="test[]" value="c5"/> <button onClick="alert(getCheckBoxesValues());">Result</button>
You have to modify you code to valid JavaScript code. 您必须将代码修改为有效的JavaScript代码。 That is:
vartoberead [vartoberead .length]
should be vartoberead[vartoberead.length]
without any spaces. 也就是说:
vartoberead [vartoberead .length]
应该是vartoberead[vartoberead.length]
没有任何空格。 Beside this issue everything else is ok with your code. 除此问题外,您的代码其他所有内容都可以。
Just by looking at your code I would rather use vartoberead.push($(this).val())
to add to the returned array than looking at the arrays length. 仅通过查看您的代码,我宁愿使用
vartoberead.push($(this).val())
将其添加到返回的数组中,而不是查看数组的长度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.