我正在尝试验证输入字段值。如果值错误,则当我在模糊和 onfocusout 上使用时,焦点应保留在同一输入字段中。请帮帮我。
这是我的代码:
$(document).ready(function(){
$("#txt_fname").on("focusout", (function(evnt){
if ($(this).val() != "name"){
event.preventDefault();
//$("#txt_fname").focus().trigger('focusin');
$("#txt_fname").focus();
return false;
}
})
);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
<input type="text" id="txt_fname" name="txt_fname" value="" placeholder="Enter a First name" maxlength="10">
<input type="text" id="txt_lname" name="txt_lname" value="" placeholder="Enter a Last name" maxlength="10">
该代码段工作正常,但在我使用 xampp
的本地主机中,它不起作用。甚至,我也改变了Jquery版本:
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
我不知道这背后的问题是什么。请帮助我。
谢谢!
最佳答案
希望这段代码能有所帮助:
$(document).on("focusout", "#txt_fname", function()
{
if ($(this).val() != "name")
{
$("#txt_fname").focus();
return false;
}
})
关于javascript - 使用 JQuery onfocusout 或 onblur 事件时如何重新聚焦于输入元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59249600/