基本上我有一个表格,当它提交时,我会在空白字段中添加一个类以突出显示它们要填写。
但是,当用户开始填写输入或更改“选择”时,我想删除此类。
这就是我正在尝试的,我的问题是我不知道如何只选择已修改的元素。我怎样才能做到这一点?
$("input, select").change(function(){
$('input').removeClass('highlight'); // Only affect modified element
});
基本上我有一个表格,当它提交时,我会在空白字段中添加一个类以突出显示它们要填写。
但是,当用户开始填写输入或更改“选择”时,我想删除此类。
这就是我正在尝试的,我的问题是我不知道如何只选择已修改的元素。我怎样才能做到这一点?
$("input, select").change(function(){
$('input').removeClass('highlight'); // Only affect modified element
});
你可以用$(this)
我猜:
$("input, select").change(function(){
$(this).removeClass('highlight'); // Only affect modified element
});
使用this
. 这是一个非常常见的 jQuery 习惯用法,包含所有事件(和许多非事件)回调。
$("input, select").change(function(){
$(this).removeClass('highlight'); // Only affect modified element
});
http://api.jquery.com/bind/#event-handlers
该
handler
参数采用回调函数,如上所示。在处理程序中,关键字this
是指处理程序绑定到的 DOM 元素。要使用 jQuery 中的元素,可以将其传递给普通$()
函数。
只需使用$(this)
. 默认情况下,jQuery 方法将使用该参数作为事件元素。如果你只想影响input
,那么:
$(this).filter("input").removeClass(...)