[英]how do I write a jQuery/javascript if statement so that it's triggered when a html <option> with a specific class is selected?
<select>
<option value='34'>Trees</option>
<option value='13' class='create_sub_category'>Add new sub-category</option>
<option value='24' class='create_sub_category'>Add new sub-category</option>
<option value='57'>Cats</option>
<option value='34' class='create_sub_category'>Add new sub-category</option>
</select>
<script type='text/javascript'>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
if (selected(".create_sub_category")) {
alert('hi');
}
</script>
Obviously, there's no such javascript or jQuery function called 'selected' that I used above in my if statement, but could someone help me find something that would do what I'm describing? 显然,我在if语句中没有上面使用过的名为“ selected”的javascript或jQuery函数,但是有人可以帮助我找到可以做我所描述的东西吗?
$('select').change(function() {
if($('select option:selected').hasClass('create_sub_category'))
{
alert("SELECTED");
}
});
You should put some id/class on that select element. 您应该在该select元素上放置一些id / class。
You can do this: 你可以这样做:
if($("select option:selected").hasClass("create_sub_category")) {
alert("hi");
}
The key here is: option:selected 这里的关键是:选项:已选择
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.