如何使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 22:47:59  来源:igfitidea点击:

How do I get the class attribute value using jquery and javascript

javascriptjqueryhtml

提问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"> &nbsp Activity</option>
    <option class="type" value="Alert"> &nbsp Alert</option>
    <option class="type" value="Lead"> &nbsp Lead</option>
    <option class="type" value="Notification"> &nbsp 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, thisis windowobject.

请注意,在startFilter函数的上下文中,thiswindow对象。

回答by frogatto

Why don't you use hasClassjQuery API? It's very good in this case:

为什么不使用hasClassjQuery API?在这种情况下非常好:

if($("option:selected", this).hasClass('type')){
    //do something
}

And make sure thisreferes 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

Fiddle Demo

小提琴演示

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"> &nbsp Activity</option>
    <option class="type" value="Alert"> &nbsp Alert</option>
    <option class="type" value="Lead"> &nbsp Lead</option>
    <option class="type" value="Notification"> &nbsp 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"> &nbsp Activity</option>
    <option class="type" value="Alert"> &nbsp Alert</option>
    <option class="type" value="Lead"> &nbsp Lead</option>
    <option class="type" value="Notification"> &nbsp Notification</option>
</select>
<script>
 function getval(sel)
{

    if($('.sele :selected').attr('class'))
    {
      console.log($('.sele :selected').attr("class","typ123"));
          // Your code write here
    }

}
</script>