简体   繁体   English

jQuery:选择的一个选项具有空值

[英]jquery : one option of a select has empty value

Here is my HTML: 这是我的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 : 当我尝试通过jquery获取select的值时,一切正常:

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

Except for label 5 (aka value 4), the log returns an empty string. 除标签5(即值4)外,日志返回一个空字符串。

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. 在记录该值之前,我检查url中的哈希并更改该值(如果存在)。 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/ 在我的提琴上尝试一下: http : //jsfiddle.net/CPLK2/

In my case everything working fine, for label 5 to 就我而言,一切正常,对于标签5

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM