$(this).focus() 在文本框为空时不起作用
$(this).focus() not working when textbox is empty
我的以下代码在文本框为空时没有设置焦点:
var empty = false;
$('.Newtxt').each(function () {
if ($.trim($(this).val()) == "") {
$(this).focus();
empty = true;
}
});
if (empty) {
return false;
}
我们可以把重点放在$(this)
元素上吗?我有多个文本框,需要用.Newtxt
类进行验证。
试试这个!
$('.Newtxt').each(function () {
if ($.trim($(this).val()) == "") {
$(this).focus();
return false; // To break out of each loop
}
});
享受!!
使用:
var empty = false;
$('.Newtxt').each(function () {
if ($.trim($(this).val()) == "") {
$(this).focus();
empty = true;
break; // add this, you have to break cycle, to focus on the first found empty element...
}
});
if (empty) {
return false;
}
最后我找到了一个解决方案。试试,让我知道
$('.Newtxt').removeClass('invalid');
$('.Newtxt').each(function () {
if ($.trim($(this).val()) == "") {
$(this).addClass('invalid');
empty = true;
}
});
if (empty) {
$(".invalid").eq(0).focus();
return false;
}
相关文章:
- jQuery(this).Pparent().Pparente().Prent().find()在iPad上不起作用
- Javascript:this.value 函数不起作用
- Javascript onchange(this.value,this.value)在ipad上不起作用
- $(this).attr({class:“activeTab”});不起作用,因为这是未定义的
- Firebase child_added“this”不起作用
- $(this) 在 onclick 属性中不起作用
- 不确定为什么使用$(this)的函数没有'不起作用
- JS this和self-don'不起作用
- jQuery this.href 字符串比较不起作用
- $(this).html 在 jquery 中不起作用
- jQuery $this 在网页上不起作用
- 滑动面板 .stop() 在 $(this).next 上不起作用
- jquery $(this).find 对我不起作用
- jQuery this.data-x 不起作用
- React-native:this.navigator.pop 不起作用
- Jquery - this.form不起作用
- $(this) 在 .append() 中不起作用
- jQuery 切换在 this.name 时不起作用
- $(“.class”).width($(this)..) 不起作用
- $this->_request->isXmlHttpRequest() 不起作用