简体   繁体   中英

reset name and the value of the combobox item

on certain event I want to change combobox selection to be reset to it's default state, and I'm using

$("select#Name").prop('selectedIndex', 0); 

try with

$("select#Name").val(0);
$("select#Name").val('0');
$("select#Name").val('');
$("select#Name").val(0).change();

this successfully reset the selected value but previous selection text remains inside combo, how to reset this too without explicitly target text property of this element.

Is there one expression to reset name and the value?

Try :

$("select#Name").val($("select#Name option:first").val());

Hope this helps.

 $('#reset').click(function(){ $("select#Name").val($("select#Name option:first").val()); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id='Name'> <option value='val_1'>Text 1</option> <option value='val_2'>Text 2</option> <option value='val_3'>Text 3</option> <option value='val_4'>Text 4</option> </select> <button id="reset">Reset</button> 

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