简体   繁体   English

jQuery:从具有相同名称的不同选择标记中获取值

[英]jQuery: get value from different select tag with same name

I was trying to get the value from different select tag with the same name using jQuery. 我试图使用jQuery从具有相同名称的不同选择标记中获取值。

The following is the HTML 以下是HTML

<div class="row">      
  <div class="col-md-3">
    <h1><i class="fa fa-glass" aria-hidden="true"></i></h1>
    <select class="form-control SelectWine" id="SelectWine" name="SelectWine[]">
      <option value="1">Red</option> <option value="2">Green</option> <option value="3">White</option>                                             </select>
  </div>
  <div class="col-md-3">
    <h1><i class="fa fa-glass" aria-hidden="true"></i></h1>
    <select class="form-control SelectWine" id="SelectWine" name="SelectWine[]">
      <option value="1">Red</option> <option value="2">Green</option> <option value="3">White</option>                                             </select>
  </div>

</div>

The following is how I am accessing in jQuery: 以下是我在jQuery中的访问方式:

$('.SelectWine').each(function (index){
       var wineId=[];
        wineId.push($(this).val());
        console.log('Wid',wineId[0]);
        console.log('Wid',wineId[1]);
        console.log('Wid',wineId[2]);
   });

I am getting console log as following 我正在获取控制台日志,如下所示

Wid 1
Wid undefined
Wid undefined
Wid 3
Wid undefined
Wid undefined

I'm guessing each time it's overriding the value. 我猜每次它都覆盖了价值。 But i need to store the values from each select tag in to one array. 但是我需要将每个选择标记中的值存储到一个数组中。

Note : the first select tag is in the default(index 1) and second one i selected the third option(index 3). 注意 :第一个选择标记位于默认值(索引1)中,第二个选择标记第三个选项(索引3)。

This code is to get the users input for 2 or 3 different products and store it into the cookie for further use, but first i need to store it as array then i am able to store as json array in the cookie. 这段代码是为了让用户输入2或3种不同的产品,并将其存储到Cookie中以供进一步使用,但是首先我需要将其存储为数组,然后才能将其存储为json数组。

I tried accessing through the name of the element but it's only responding for accessing using the id. 我尝试通过元素的名称进行访问,但它仅响应使用id进行的访问。

I was trying for hours, can't figure out a way. 我试了几个小时,想不通办法。 your help will be great !! 您的帮助将是伟大的!

You need to declare the array outside the loop, otherwise you are creating a new array in each iteration and don't have access to it outside of the each callback due to scope. 您需要在循环外部声明该数组,否则您将在每次迭代中创建一个新数组,并且由于范围的原因,您将无法在each回调之外访问该数组。

var wineId=[];
$('.SelectWine').each(function (index){
       //now each iteration will push to same array
       ....

When you need to generate an array like this you can use map() 当您需要生成这样的数组时,可以使用map()

var windeId = $('.SelectWine').map(function(){
    return this.value;
}).get();

The main problem is that you are declaring your array inside the for each, so it is getting overwritten. 主要问题是您要在每个数组中声明数组,因此它会被覆盖。 Declare it outside the loop. 在循环外声明它。

Also, unrelated to your problem: 另外,与您的问题无关:

  • Your select elements have the same name and id. 您选择的元素具有相同的名称和ID。 They can and should share a class name, but not element name and id. 他们可以并且应该共享一个类名,但不能共享元素名和ID。
  • You can just use 'this.value' rather than '$(this).val()'. 您可以只使用'this.value'而不是'$(this).val()'。 Avoiding the unnecessary use of jQuery when possible will improve performance. 尽可能避免不必要地使用jQuery将提高性能。

https://jsfiddle.net/1ok6ymgf/ https://jsfiddle.net/1ok6ymgf/

var wineId=[];
$('.SelectWine').each(function (index) {      
    wineId.push(this.value);
    console.log('Wid',wineId[0]);
    console.log('Wid',wineId[1]);
    console.log('Wid',wineId[2]);
});

具有不同的ID并使用$('#id_name')调用javascript或具有相同的类名并使用$('.class_name')调用javascript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM