如何使用 jquery 和 javascript 获取类属性值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18933408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the class attribute value using jquery and javascript
提问by user2671355
I'm trying to get the class of the selected option and use that in a compare statement to find which class it is.
我正在尝试获取所选选项的类并在比较语句中使用它来查找它是哪个类。
I get an undefined value. The option tag has a class set to it.
我得到一个未定义的值。选项标签设置了一个类。
Why am I not getting a value back?
为什么我没有取回价值?
Html:
网址:
<select name="datagrid_filter" onchange="startFilter();">
<option>All</option>
<option disabled="true">Assignment:</option>
<option disabled="true">Client:</option>
<option disabled="true">Type:</option>
<option class="type" value="Activity">   Activity</option>
<option class="type" value="Alert">   Alert</option>
<option class="type" value="Lead">   Lead</option>
<option class="type" value="Notification">   Notification</option>
</select>
Javascript:
Javascript:
var cs1 = $("option:selected", this).attr("class");
if(cs1 == 'Type'){
//do something
}
回答by Tho
You should change:
你应该改变:
onchange="startFilter();"
to onchange="startFilter(this);"
onchange="startFilter();"
到 onchange="startFilter(this);"
and in startFilterfunction:
在startFilter函数中:
function startFilter(ele){
var cs1 = $("option:selected", ele).attr("class");
if(cs1 == 'type'){
//do something
}
}
Note that, in the context of startFilterfunction, this
is window
object.
请注意,在startFilter函数的上下文中,this
是window
对象。
回答by frogatto
Why don't you use hasClass
jQuery API? It's very good in this case:
为什么不使用hasClass
jQuery API?在这种情况下非常好:
if($("option:selected", this).hasClass('type')){
//do something
}
And make sure this
referes to a proper object (as @undefined mentioned)
Or use:
并确保this
引用正确的对象(如@undefined 所述)
或使用:
if($("option:selected", "select[name=datagrid_filter]").hasClass('type')){
//do something
}
Or:
或者:
if($("select[name=datagrid_filter] option:selected").hasClass('type')){
//do something
}
回答by Tomas Santos
Try not to have inline JavaScript
尽量不要内联 JavaScript
JavaScript
JavaScript
$(function()
{
var handleChange = function()
{
var $this = $(this);
var $selected = $this.children(":selected");
var isType = $selected.hasClass('type');
if(isType)
{
alert('Do Something');
}
};
var $datagridFilter = $('[name=datagrid_filter]')
$datagridFilter.change(handleChange);
});
回答by nirali
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select onchange="getval(this);" class="sele"> <option>All</option> <option disabled="true">Assignment:</option> <option disabled="true">Client:</option> <option disabled="true">Type:</option> <option class="type" value="Activity">   Activity</option> <option class="type" value="Alert">   Alert</option> <option class="type" value="Lead">   Lead</option> <option class="type" value="Notification">   Notification</option> </select> <script> function getval(sel) { if($('.sele :selected').attr('class')) { console.log($('.sele :selected').attr("class","typ123")); // Your code write here } } </script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select onchange="getval(this);" class="sele"> <option>All</option> <option disabled="true">Assignment:</option> <option disabled="true">Client:</option> <option disabled="true">Type:</option> <option class="type" value="Activity">   Activity</option> <option class="type" value="Alert">   Alert</option> <option class="type" value="Lead">   Lead</option> <option class="type" value="Notification">   Notification</option> </select> <script> function getval(sel) { if($('.sele :selected').attr('class')) { console.log($('.sele :selected').attr("class","typ123")); // Your code write here } } </script>