admin管理员组文章数量:1415673
When the user delete the value in input using the backspace
the function work correctly but if he delete the value and set a new number like 1 the new value is deleted and set again to zero.
<input type="text" id="txtField" value="0" ></input>
$(document).ready(function() {
$("#txtField").keyup(function() {
var v = $(this).val();
console.log(v)
if (v == "") {
console.log(v)
setTimeout(function() {
$("#txtField").val(0);
}, 1000);
}
});
});
I don't know what is the problem in my code any help please?
jsFiddle: /
When the user delete the value in input using the backspace
the function work correctly but if he delete the value and set a new number like 1 the new value is deleted and set again to zero.
<input type="text" id="txtField" value="0" ></input>
$(document).ready(function() {
$("#txtField").keyup(function() {
var v = $(this).val();
console.log(v)
if (v == "") {
console.log(v)
setTimeout(function() {
$("#txtField").val(0);
}, 1000);
}
});
});
I don't know what is the problem in my code any help please?
jsFiddle: http://jsfiddle/2shadgtw/2/
Share Improve this question edited Dec 7, 2015 at 9:36 Praveen Kumar Purushothaman 167k27 gold badges213 silver badges260 bronze badges asked Dec 7, 2015 at 9:29 SoraSora 2,55119 gold badges77 silver badges151 bronze badges5 Answers
Reset to default 3Don't use setTimeout
. Instead try mapping with these three events:
$(document).ready(function() {
$("#txtField").on("keyup change blur", function() {
if ($(this).val().trim() == "")
$(this).val(0);
});
});
Fiddle: http://jsfiddle/5hm6e8hL/
I would use a blur
event to do this rather than keyup
:
$(document).ready(function() {
$("#txtField").on("blur", function() {
if (!$(this).val().trim())
$(this).val(0);
});
});
If the input is empty when it loses focus - then give it a value 0.
else - do nothing.
FIDDLE
The problem is that you have setTimeout
. If the user clears the input
and then enters a value in the next second, the function in setTimeout
will still go off, setting the value
to 0
.
Try checking the input
inside the setTimeout
instead, like this:
JavaScript
$("#txtField").keyup(function() {
setTimeout(function() {
if ($("#txtField").val().trim().length === 0) {
$("#txtField").val(0);
}
}, 1000);
});
JSFiddle
setTimeout
is a bad solution. But ypu can use if-statement:
$(document).ready(function() {
$("#txtField").keyup(function() {
var v = $(this).val();
console.log(v)
if (v == "") {
console.log(v)
setTimeout(function() {
if(!$("#txtField").val()){
$("#txtField").val(0);
}
}, 1000);
}
});
});
http://jsfiddle/guv0c5ox/
The best way to do this would be using focusout() function. Try this
<input type="text" id="txtField" value="0" />
$("#txtField").focusout(function() {
var v = $(this).val();
console.log(v)
if (v == "") {
$("#txtField").val(0);
}
});
It will set the value to zero on focus out if the field is empty.
JSFIDDLE EXAMPLE
本文标签: check if input is empty set to zero in javascriptStack Overflow
版权声明:本文标题:check if input is empty set to zero in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745208770a2647765.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论