admin管理员组文章数量:1332889
I have an if statement where I'm trying to set a variable if the selected value of a select element does not contain "ALL"
Here's the code I've tried, but it always returns "%"...
if(!$("select.mySelect option:selected").filter(":contains('ALL')")) {
selected_option = $("select.mySelect option:selected");
} else {
selected_option = "%";
}
HTML
<select class="mySelect">
<option value="ALLTYPE1">All type 1</option>
<option value="CAR">Car</option>
<option value="VAN">Van</option>
<option value="ALLTYPE2">All type 2</option>
<option value="PLANE">Plane</option>
<option value="HELICOPTER">Helicopter</option>
</select>
Any ideas?
I have an if statement where I'm trying to set a variable if the selected value of a select element does not contain "ALL"
Here's the code I've tried, but it always returns "%"...
if(!$("select.mySelect option:selected").filter(":contains('ALL')")) {
selected_option = $("select.mySelect option:selected");
} else {
selected_option = "%";
}
HTML
<select class="mySelect">
<option value="ALLTYPE1">All type 1</option>
<option value="CAR">Car</option>
<option value="VAN">Van</option>
<option value="ALLTYPE2">All type 2</option>
<option value="PLANE">Plane</option>
<option value="HELICOPTER">Helicopter</option>
</select>
Any ideas?
Share Improve this question asked Aug 15, 2013 at 10:11 TomTom 13k50 gold badges153 silver badges247 bronze badges 1- Please provide the corresponding HTML. – MCL Commented Aug 15, 2013 at 10:12
1 Answer
Reset to default 3Your if
statement is incorrect. .filter()
will never return a false value - it'll always be a jQuery collection - which can be empty, but never false. To test it there are any elements in the jQuery collection returned by .filter()
, use the .length
property.
Use:
if($("select.mySelect option:selected").filter(":contains('ALL')").length === 0) {
...
JSFiddle here: http://jsfiddle/egEX8/
本文标签: javascriptjQueryselected option value contains stringStack Overflow
版权声明:本文标题:javascript - jQuery - selected option value contains string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742317306a2452075.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论