简体   繁体   English

如何使用jQuery从选择框中清除所选值?

[英]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 HTML代码

<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 JS代码

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

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

暂无
暂无

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

相关问题 如何使用jquery更改标签的文本,使其与从选择框中选择的值相同? - How do change the text of a label using jquery to make it the same as the value selected from a select box? 如何使用 jquery 在 select 选项中设置选定值? - How do I to set selected value in a select option using jquery? 使用jQuery从选择框中删除选择的值,但需要在选择框中显示当前选择的值吗? - remove selected value from select box using jquery but It's need to display current selected value in select box? 选择框中的jquery“清除”值 - jquery "clear" value in select box 如何在文档加载时使用jquery在选择框中选择特定项目 - How do i make the particular item selected in select box using jquery on document load 如何从iframe内部的选择框中获取所选值,并将其显示在外部? - How do I get the selected value from a select box, from inside an Iframe, and display it on the outside? 使用jQuery从另一个选择框的选定值中显示和隐藏一个选择框的选项 - using jQuery show and hide options of one select box from the value selected of another select box 如何从选择框中获取选定的值,并将其用于HTML链接? - How do I get the selected value from a select box, and use it in a HTML link? 我如何使用jQuery从选择框中获取一堆选定的ID - How can I get bunch of selected ids from select box using jquery 如何使用 jquery 清除“选择”的内容? - How do i clear the contents of a “select” using jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM