当我跳过文本框时,我会收到警报,但无论我点击页面,也会收到警报。当焦点位于文本框上时如何显示警报?即使我打开新标签页,警报也会出现。有办法解决这个问题吗?非常感谢。
$(function() {
$("input").blur(function() {
if(!this.value) {
alert("Text box " + ($(this).index() + 1) + " cannot be empty.");
this.focus(); //Keep focus on this input
}
});
正文部分
<input type="text" id="textbox1"/>
<input type="text" id="textbox2"/>
<input type="text" id="textbox3"/>
<input type="text" id="textbox4"/>
最佳答案
$(function() {
$('input:text').blur(function() {
if(!this.value) {
alert("Text box " + ($(this).index() + 1) + " cannot be empty.");
this.focus(); //Keep focus on this input
}
});
});
另请访问http://api.jquery.com/text-selector/
希望有帮助。
关于javascript - 仅当焦点位于文本框中时发出警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9478138/