2014-04-08 25 views
0

我在表单中有很多文本框。当其各自的值未通过验证时,将应用一个班级“有错误”,这会向文本框添加红色边框。捕获所有文本框的焦点事件 - jQuery

我想撤消这个类,如果该文本框获得焦点。

喜欢的东西

$(document).on('focus',find(':input'),function(){ 
    $(this).removeClass('has-error'); 
}); 

这是如何完成的?

+0

谢谢大家。爱德华多的解决方案工作。但它被删除了。有任何问题。 '$(“input:text”)。focus(function(){$(this).removeClass('has-error')});' – Ruby

回答

3

你不需要find(':input')只是使用':input.has-error'

$(document).on('focus', ':input.has-error' ,function(){ 
    $(this).removeClass('has-error'); 
}); 

,并且确保你的代码DOM Ready

+1

值得注意的是焦点事件不会冒泡,jQuery只是伪造它。 – adeneo

+0

是的,它已经准备好了 – Ruby

+0

@Ruby你可以做一个http://jsfiddle.net/? –

1

只要做到:

$(document).on("focus", "input:text.has-error", function() { 
+0

这不完全相同,我不确定OP的需求,但它不适用于动态加载的元素AFAIK。 – Trufa

+0

@Trufa - 类是基于验证应用的 - 这可以解决这个问题。 – tymeJV

+0

不,他们不会。 http://jsfiddle.net/vcd2X/1/ http://jsfiddle.net/vcd2X/2/ – Trufa