jquery - Reset textbox value and display warning message
i have a textbox and when i enter 0 in it, it should reset to 1 and display warning message.
i tried with blur() and change(), it is working but the problem is, it is displaying warning on coming out of focus from textbox after entering 0..
what i need is as soon as i enter 0 in textbox the warning msg should come
i have attached code what i tried
开发者_JS百科$("#textbox1").change(function(){
textboxfunction();
});
function textboxfunction(){
($("#textbox1").val() == 0) ? $("#warning").show() && $("#textbox1").val(1) : $("#warning").hide()
}
You need the keyup event instead, I think:
$("#textbox1").keyup(function(){
textboxfunction();
});
As Mark B mentioned, you need to use keyup
, but you also need to put the 0
in quotes:
$("#textbox1").val() == "0"
The string "0"
is different than the number 0
, especially when ==
is involved!
精彩评论