Get the value of a dropdown in jQuery

后端 未结 10 2245
臣服心动
臣服心动 2020-11-29 02:46

I have a drop down that has an \'ID, Name\' Pair.

Example

Jon Miller
Jim Smith
Jen Morsin

Jon MIller has ID of 101
Jim Smith has ID of 10

相关标签:
10条回答
  • Try this:

    var text = $('#YourDropdownId').find('option:selected').text();
    
    0 讨论(0)
  • 2020-11-29 03:00

    $('.leave_response').on('change', function() {
        var responseId = $(this).val();
        console.log(responseId);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <td>
        <select class="leave_response" name="leave">
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
        </select>
    </td>

    0 讨论(0)
  • 2020-11-29 03:01

    If you're using a <select>, .val() gets the 'value' of the selected <option>. If it doesn't have a value, it may fallback to the id. Put the value you want it to return in the value attribute of each <option>

    Edit: See comments for clarification on what value actually is (not necessarily equal to the value attribute).

    0 讨论(0)
  • 2020-11-29 03:02

    This has worked for me!

    $('#selected-option option:selected').val()
    

    Hope this helps someone!

    0 讨论(0)
  • 2020-11-29 03:03
    var sal = $('.selectSal option:selected').eq(0).val();
    

    selectSal is a class.

    0 讨论(0)
  • 2020-11-29 03:05

    As has been pointed out ... in a select box, the .val() attribute will give you the value of the selected option. If the selected option does not have a value attribute it will default to the display value of the option (which is what the examples on the jQuery documentation of .val show.

    you want to use .text() of the selected option:

    $('#Crd option:selected').text()

    0 讨论(0)
提交回复
热议问题