简体   繁体   中英

jquery : one option of a select has empty value

Here is my HTML:

<select name="category" class="tri_cat">
<option value="">classer par catégories</option>
<option value="1">label 1</option>
<option value="2">label 2</option>
<option value="9">label 3</option>
<option value="3">label 4</option>
<option value="4">label 5</option>
<option value="5">label 6</option>
<option value="6">label 7</option>
<option value="7">label 8</option>
<option value="8">label 9</option>
</select>

When I try to get the value of the select with jquery, everything works fine :

console.log($('.tri_cat').val());

Except for label 5 (aka value 4), the log returns an empty string.

In other cases, the log returns the correct value.

Before logging the value, I check a hash in the url and change the value if one exists. It's the only moment where I'm messing with the input value:

$('.tri_cat').val(hash);

It's a total mystery for me :s

I try with this fiddle, adding a button to control when log the value:

$("#btn").click(function(){
  console.log($('.tri_cat').val());
});

Try it on my fiddle: http://jsfiddle.net/CPLK2/

In my case everything working fine, for label 5 to

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