Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

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

(我试图获取所选选项的类,并在compare语句中使用它来查找它是哪个类。)

I get an undefined value.

(我得到一个未定义的值。)

The option tag has a class set to it.

(option标记设置了一个类。)

Why am I not getting a value back?

(为什么我没有找回价值?)

Html:

(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
}
  ask by user2671355 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should change:

(您应该更改:)

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

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

and in startFilter function:

(并在startFilter函数中:)

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

Note that, in the context of startFilter function, this is window object.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...