Home > Web Front-end > JS Tutorial > How to get value of selected option in select box using jQuery

How to get value of selected option in select box using jQuery

清浅
Release: 2019-01-16 14:05:47
Original
2657 people have browsed it

You can use the selected selector in jQuery to get the selected option element. We can also select options from the multi-select box

How to get value of selected option in select box using jQuery

In jQuery: The selected selector is mainly used to select the selected

Example:

HTML code:

  <form>
        <label>选择城市:</label>
        <select class="country">
            <option value="合肥">合肥</option>
            <option value="上海">上海</option>
            <option value="北京">北京</option>
        </select>
    </form>
Copy after login

jQuery code:

$(document).ready(function(){
    $("select.country").change(function(){
        var selectedCountry = $(this).children("option:selected").val();
        alert("你选择的城市是: " + selectedCountry);
    });
});
Copy after login

The rendering is as follows:

How to get value of selected option in select box using jQuery

Selecting options from a multi-select box

Multi-select boxes allow the user to select multiple options. Multiple sections can be enabled in the select box by adding the multiple attribute to the