简体   繁体   中英

jQuery get the <select> value rather than the selected <option> value

In the above example the console logs a value of 1 which is the currently selected option. How can you log the <select> tag's value of 2?

 $(document).ready(function() { console.log($('.mySelect').val()); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select class="mySelect" value="2"> <option value="1" selected>Choice 1</option> <option value="2">Choice 2</option> <option value="3">Choice 3</option> </select> 

This is not valid HTML syntax.

Use data attributes like data-someattr for values you want to pass.

这将为您工作。

console.log( $('.mySelect[value=2]').val() );

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