简体   繁体   中英

How do I clear the selected value from a select box using jQuery?

I don't want to remove the options, just clear the selected value.

I tried the following:

$("#myid").val("")

and that does not seem to work.

你可以使用prop方法。

$("#myid option:selected").prop("selected", false)

只有当你有一个值的选项时,这才有效:

<option value=""></option>

You can try this

HTML CODE

<select id="myid">
    <option value="">Select None</option>
    <option value="0" id="item1">Item 1</option>
    <option value="1" id="item1">Item 2</option>
    <option value="2" id="item2">Item 3</option>
    <option value="3" id="item3">Item 4</option>
</select>

JS CODE

$(document).ready(function(){
    $('#myid').change(function(){
        var getVal= $('#myid').select().attr('value');
$('option[id=item'+getVal+']').attr('value','');
    });
});

http://jsfiddle.net/RtvPz/63/

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