简体   繁体   English

如何使用 jquery 和 javascript 获取类属性值

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

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
}

You should change:你应该改变:

onchange="startFilter();" to onchange="startFilter(this);"onchange="startFilter(this);"

and in startFilter function:startFilter函数中:

function startFilter(ele){
    var className = $("option:selected", ele).attr("class");
    if(className == 'type'){
        //do something
    }
}

Take note that, the this inside startFilter function refers to the window object, not the element you bind it to.请注意, startFilter函数中的this是指window对象,而不是您绑定到的元素。

Why don't you use hasClass jQuery API?为什么不使用hasClass jQuery API? It's very good in this case:在这种情况下非常好:

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

And make sure this referes to a proper object (as @undefined mentioned)并确保this引用正确的对象(如@undefined 所述)
Or use:或使用:

if($("option:selected", "select[name=datagrid_filter]").hasClass('type')){
        //do something
    }

Or:要么:

if($("select[name=datagrid_filter] option:selected").hasClass('type')){
            //do something
 }

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);

    });
<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>

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

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