返回值数组的Javascript函数

时间:2022-08-19 09:52:02

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

感谢每一位帮助

6 个解决方案

#1


7  

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();
}

#2


2  

You can use map:

你可以使用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用返回的值为你填充数组。

#3


0  

you should return after the each() cycle (and not inside the cycle as you're doing) anyway you could also use serializeArray

你应该在每个()循环后返回(而不是在你正在做的循环内),无论如何你也可以使用serializeArray

function getcheckboxed() {
   return $("input:checkbox[name=test]:checked").serializeArray();
};

#4


0  

When you print the value inside de each ( console.log($(this).val(), vartoberead); ) it show the values correctly ?

当您在de each(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;
}

#5


0  

I have a pure javascript solution for this . I have used html array i.e all checkboxes having same name (say test[])

我有一个纯粹的JavaScript解决方案。我使用了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>

#6


0  

You have to modify you code to valid JavaScript code. That is: vartoberead [vartoberead .length] should be vartoberead[vartoberead.length] without any spaces. Beside this issue everything else is ok with your code.

您必须将代码修改为有效的JavaScript代码。那就是:vartoberead [vartoberead .length]应该是vartoberead [vartoberead.length],没有任何空格。除了这个问题,其他一切都可以用你的代码。

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())添加到返回的数组而不是查看数组长度。

#1


7  

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();
}

#2


2  

You can use map:

你可以使用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用返回的值为你填充数组。

#3


0  

you should return after the each() cycle (and not inside the cycle as you're doing) anyway you could also use serializeArray

你应该在每个()循环后返回(而不是在你正在做的循环内),无论如何你也可以使用serializeArray

function getcheckboxed() {
   return $("input:checkbox[name=test]:checked").serializeArray();
};

#4


0  

When you print the value inside de each ( console.log($(this).val(), vartoberead); ) it show the values correctly ?

当您在de each(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;
}

#5


0  

I have a pure javascript solution for this . I have used html array i.e all checkboxes having same name (say test[])

我有一个纯粹的JavaScript解决方案。我使用了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>

#6


0  

You have to modify you code to valid JavaScript code. That is: vartoberead [vartoberead .length] should be vartoberead[vartoberead.length] without any spaces. Beside this issue everything else is ok with your code.

您必须将代码修改为有效的JavaScript代码。那就是:vartoberead [vartoberead .length]应该是vartoberead [vartoberead.length],没有任何空格。除了这个问题,其他一切都可以用你的代码。

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())添加到返回的数组而不是查看数组长度。