Javascript 将焦点设置在textbox的onChange事件上
若结果是错误的,我想将焦点设置在文本框本身的更改事件上。假设我的文本框id为“txtCaseCode”Javascript 将焦点设置在textbox的onChange事件上,javascript,jquery,Javascript,Jquery,若结果是错误的,我想将焦点设置在文本框本身的更改事件上。假设我的文本框id为“txtCaseCode” $('#txtCaseCode').change(function() { if(condition does not satsfy) { alert('Wrong ID'); $('#txtCaseCode').focus(); } } 但它不起作用。我将如何实现这一点 以下是附加模糊事件后的代码: $('#txtCase
$('#txtCaseCode').change(function() {
if(condition does not satsfy)
{
alert('Wrong ID');
$('#txtCaseCode').focus();
}
}
但它不起作用。我将如何实现这一点
以下是附加模糊事件后的代码:
$('#txtCaseCode').change(function() {
if(condition does not satsfy)
{
alert('Wrong ID');
$('#txtCaseCode').blur(function(){
$('#txtCaseCode').focus();//this is not working
$('#txtCaseCode').val('Please click here.');//this is not working
});
}
else
{
execute code;
}
}
更改要附加到的事件
blur
。这样,每当文本框失去焦点时,就可以将焦点返回到它
change
事件发生在blur
之前,因此,对聚焦事件调用focus()
不会产生任何效果。请尝试以下操作:
var $txt = $("#txtEmail").focus();
$txt.blur(function(){
if(!validateEmpty(this)){
$(this).focus();
}
});
function validateEmpty(el){
var flag = true,
v = $(el).val();
if(v == ""){
alert("Can't Leave Blank");
flag = false;
}
return flag;
}
在这里演奏小提琴:
我希望这会有所帮助。发布完整代码,如果条件为false,则永远不要警告显示当onchange事件触发时,您已经将焦点放在文本框上。什么类型的元素是
“#txtCaseCode”
?是
还是
?或
?我尝试在错误消息后附加模糊事件。但它仍然不起作用。@Mota Chuha:是的,当页面加载时,我已经关注这个文本框了。