简体   繁体   中英

How to get Value of multiple select options with same class and name

I am trying to get the value of the selected option. However, there are 2 different select menus, using the same name and class. I want to be able to pull the text inside the value.

I already know which select option they choose, but I can't seem to get the value of the option.

This is the HTML

<div id="single_course" style="display: none;">
    <SELECT class="form-control" name="course_choice">
        <OPTION value="1">Value 1</OPTION>
        <OPTION value="2">Value 2</OPTION>
        <OPTION value="3">Value 3</OPTION>
    </SELECT>
</div>
<div id="2day_course" style="display: none;">
    <SELECT class="form-control" name="course_choice">
        <OPTION value="1">Value 1</OPTION>
        <OPTION value="2">Value 2</OPTION>
        <OPTION value="3">Value 3</OPTION>
    </SELECT>
</div>

This the JS Code

var course_choice = $('#register select[name=course_choice]').value;

Your advice is greatly appreciated!!

First of all, you cannot get the values of both. So, you can get it as an array. You can do like this:

<div id="single_course" style="display: none;">
    <SELECT class="form-control" name="course_choice[]">
        <OPTION value="1">Value 1</OPTION>
        <OPTION value="2">Value 2</OPTION>
        <OPTION value="3">Value 3</OPTION>
    </SELECT>
</div>
<div id="2day_course" style="display: none;">
    <SELECT class="form-control" name="course_choice[]">
        <OPTION value="1">Value 1</OPTION>
        <OPTION value="2">Value 2</OPTION>
        <OPTION value="3">Value 3</OPTION>
    </SELECT>
</div>

Or, if you cannot change the HTML, then you can do this way suggested by sjkm :

var course_choices = $('select[name="course_choice"]');

var course_choice[0] = course_choices.eq(0).val();
var course_choice[1] = course_choices.eq(1).val();
var course_choices = $('select[name="course_choice"]');

var course_choice1 = course_choices.eq(0).val();
var course_choice2 = course_choices.eq(1).val();

Try this:

// array of selected options (array items are dom objects)
var course_choice = $('#register select[name=course_choice] option:selected');

// extract option's value with jquery
console.log( $(course_choice[0]).val() );
console.log( $(course_choice[1]).val() );

// ... or without query
console.log( course_choice[0].value );
console.log( course_choice[1].value );

working fine for me :

    <SELECT class="form-control select_addon" name="course_choice[]">
    var mang_addon_cart = [];
    var course_choice = $('.select_addon option:selected');
    for(var i = 0; i < course_choice.length; i++){
        var val_addon = course_choice.eq(i).val();
        var name_addon = course_choice.eq(i).data('name');
      mang_addon_cart.push({
        giatri : val_addon,
        name : name_addon
      });
    }
    console.log(mang_addon_cart);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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