Jquery 如何从all selectbox选项中获取类?
我试图从当前选择的选项中获取类并插入文本框,但我只能获取第一个类: JSFiddle: HTML 我听说新的JQuery版本不再使用live,我不知道这是不是真的,但我想替换这个事件Jquery 如何从all selectbox选项中获取类?,jquery,Jquery,我试图从当前选择的选项中获取类并插入文本框,但我只能获取第一个类: JSFiddle: HTML 我听说新的JQuery版本不再使用live,我不知道这是不是真的,但我想替换这个事件 谢谢。您需要在更改处理程序中找到类 $("#templates").live("change", function () { //you need to read the class attribute inside the change handler so that you will get the c
谢谢。您需要在更改处理程序中找到类
$("#templates").live("change", function () {
//you need to read the class attribute inside the change handler so that you will get the currently selected option
var className = $(this).find('option:selected').attr('class');
$(".log_temp_id").val(className);
});
演示:
方法是,如果您希望对更改事件处理程序使用事件委派支持,请使用jQuery>=1.7的方法,尝试在jQuery版本>=1.7中使用.on()
作为.live()
的替代方法
$(document).on("change","#templates", function () {
$(".log_temp_id").val($(this).find('option:selected').attr('class'));
});
您必须在dropdwon的更改事件中选择类,如下所示:
$("#templates").live("change", function() {
var Element= $(this).find('option:selected').attr('class');
$(".log_temp_id").val(Element);
});
而.live
在jQuery版本1.7
之后被弃用,并从版本1.9
中删除。更多信息请点击
@RahulGupta,对不起,我错给了一些其他的jsfiddle。现在更新。谢谢:)
$(document).on("change","#templates", function () {
$(".log_temp_id").val($(this).find('option:selected').attr('class'));
});
$("#templates").live("change", function() {
var Element= $(this).find('option:selected').attr('class');
$(".log_temp_id").val(Element);
});