Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将每个HTML元素放入javascript数组中_Javascript_Jquery_Loops_Foreach - Fatal编程技术网

将每个HTML元素放入javascript数组中

将每个HTML元素放入javascript数组中,javascript,jquery,loops,foreach,Javascript,Jquery,Loops,Foreach,在我的网页上,我有3个选择按钮,名称为“seacheoen”。我想知道选择了哪些,并将值作为数组返回到我的网页中 此时此刻,我有以下代码: var seizoen_array = $.each($(form).children("input.selected[name=seizoen]"), function(index, evt) { if(typeof(seizoen)=="undefined") { var seizoen = []; } seizoen[index] = $(

在我的网页上,我有3个选择按钮,名称为“seacheoen”。我想知道选择了哪些,并将值作为数组返回到我的网页中

此时此刻,我有以下代码:

var seizoen_array = $.each($(form).children("input.selected[name=seizoen]"), function(index, evt) {
   if(typeof(seizoen)=="undefined") { var seizoen = []; }
   seizoen[index] = $(evt).val();                                   
});
alert(seizoen);
alert(seizoen_array);
但这不起作用,因为循环中的变量不能在循环后显示。 我找了很多,但找不到解决办法。有人能帮我吗?;-)


**对不起,我的英语不好,我希望它足够清晰,可以理解…

我建议使用jQuery:

// this selects the <input> elements, with the class-name of
// 'selected' and the name of 'seizon', from within the <form>
// and then uses the map() method to form a map:
var seizoen_array = $('form input.selected[name=seizon]').map(function () {
    // returning the value of the current element of the collection
    // over which we're iterating to the map:
    return this.value;
// using get() to convert the map to an array:
}).get();
//选择元素,类名为
//“选定”和“扣押”的名称,从
//然后使用map()方法形成一个映射:
var seashoen_array=$('form input.selected[name=seashoon]').map(函数(){
//返回集合的当前元素的值
//我们在上面迭代到地图:
返回此.value;
//使用get()将映射转换为数组:
}).get();
或者,使用纯JavaScript:

// selects the <input> elements, with the class-name of
// 'selected' and the name of 'seizoen' to form a
// collection:
var seizoenElements = document.querySelectorAll('input.selected[name=seizoen]'),

// converts the collection to an Array (using Array.from()),
// iterates over that array of elements to form a new Array
// using Array.prototype.map() to return a new Array from the
// original:
    seizoen_array = Array.from(seizoenElements).map(function(el) {

        // if there is a value and the trimmed value has a
        // non-zero length:
        if (el.value && el.value.trim().length) {

            // returns the value to the new Array if
            // the above conditions are met:
            return el.value;
        }
    });
//选择元素,类名为
//“已选定”和“seashoen”的名称构成
//收藏:
var seashoelements=document.querySelectorAll('input.selected[name=seashoen]'),
//将集合转换为数组(使用Array.from()),
//迭代该元素数组以形成新数组
//使用Array.prototype.map()从
//原件:
捕捉元素数组=数组.from(捕捉元素).map(函数(el){
//如果存在一个值且修剪后的值具有
//非零长度:
if(el.value&&el.value.trim().length){
//如果需要,则将值返回到新数组
//满足上述条件:
返回el.value;
}
});
参考资料:

  • JavaScript:
  • jQuery:

我建议使用jQuery:

// this selects the <input> elements, with the class-name of
// 'selected' and the name of 'seizon', from within the <form>
// and then uses the map() method to form a map:
var seizoen_array = $('form input.selected[name=seizon]').map(function () {
    // returning the value of the current element of the collection
    // over which we're iterating to the map:
    return this.value;
// using get() to convert the map to an array:
}).get();
//选择元素,类名为
//“选定”和“扣押”的名称,从
//然后使用map()方法形成一个映射:
var seashoen_array=$('form input.selected[name=seashoon]').map(函数(){
//返回集合的当前元素的值
//我们在上面迭代到地图:
返回此.value;
//使用get()将映射转换为数组:
}).get();
或者,使用纯JavaScript:

// selects the <input> elements, with the class-name of
// 'selected' and the name of 'seizoen' to form a
// collection:
var seizoenElements = document.querySelectorAll('input.selected[name=seizoen]'),

// converts the collection to an Array (using Array.from()),
// iterates over that array of elements to form a new Array
// using Array.prototype.map() to return a new Array from the
// original:
    seizoen_array = Array.from(seizoenElements).map(function(el) {

        // if there is a value and the trimmed value has a
        // non-zero length:
        if (el.value && el.value.trim().length) {

            // returns the value to the new Array if
            // the above conditions are met:
            return el.value;
        }
    });
//选择元素,类名为
//“已选定”和“seashoen”的名称构成
//收藏:
var seashoelements=document.querySelectorAll('input.selected[name=seashoen]'),
//将集合转换为数组(使用Array.from()),
//迭代该元素数组以形成新数组
//使用Array.prototype.map()从
//原件:
捕捉元素数组=数组.from(捕捉元素).map(函数(el){
//如果存在一个值且修剪后的值具有
//非零长度:
if(el.value&&el.value.trim().length){
//如果需要,则将值返回到新数组
//满足上述条件:
返回el.value;
}
});
参考资料:

  • JavaScript:
  • jQuery:

您应该查看JavaScript中变量作用域的工作原理。如果希望变量在函数外部可访问,则需要在函数外部定义它。例如:

var seizoen = [];
var seizoen_array = $.each($(form).children("input.selected[name=seizoen]"), function(index, evt) {
   seizoen[index] = $(evt).val();                                   
});
alert(seizoen);
alert(seizoen_array);

这样做是为了演示变量作用域是如何工作的,但您应该使用David的答案,因为这是更好的形式。

您应该在JavaScript中查找变量作用域是如何工作的。如果希望变量在函数外部可访问,则需要在函数外部定义它。例如:

var seizoen = [];
var seizoen_array = $.each($(form).children("input.selected[name=seizoen]"), function(index, evt) {
   seizoen[index] = $(evt).val();                                   
});
alert(seizoen);
alert(seizoen_array);

这样做是为了演示变量作用域是如何工作的,但是您应该使用David的答案,因为它是更好的形式。

您将所选元素放在
each()
方法之前。另外,我不确定嵌套数组是否是您真正想要的,但我认为这是您想要的输出(可能)


将所选元素放在
each()
方法之前。另外,我不确定嵌套数组是否是您真正想要的,但我认为这是您想要的输出(可能)


数组名称被混淆有什么特殊原因吗?看起来很像一号的me@AndrueAnderson:或者可能只是“季节”的荷兰语。数组名称被混淆有什么特殊原因吗?看起来很像一号的me@AndrueAnderson:或者它可能只是“季节”的荷兰语。也可以在
$(“表单输入.选定的[name=seathon]”)上使用
$.map()
并删除对
的调用。get()
也可以在
$(“表单输入.选定的[name=seathon]”上使用
$.map()
并删除对
.get()的调用