简体   繁体   中英

jQuery .val() is returning .text() not .val()

I have this code

 <select id="month"> <option val='0'>January</option> <option val='1'>February</option> </select> 

I'm using this to try to get the values:

$month = $("#month option:selected").val();

But it's returning the text "January" and "February" not "0" or "1"

I tried this and got the same results:

$month = $("#month").val();

The attribute should be value , not val :

<option value='0'>January</option>
<option value='1'>February</option>

When an option doesn't have a value attribute, the text is used as the default.

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