javascript 如果空白或为空,如何将文本框的值设置为 0?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27299997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:17:54  来源:igfitidea点击:

How can I set the value of textbox to 0 if blank or empty?

javascriptjquery

提问by Euridice01

I have the following snippet in my js file:

我的 js 文件中有以下代码段:

if ($("#LengthenCheckBox").is(":checked")) {
                $("#Length").data(kendoTextBox).enable(true);

                //If Lengthening field is blank, default to zero
                if ($("#Length").data(kendoTextBox) == "") {
                    $("#Length").data(kendoTextBox).value("0");
                };

                document.getElementById("Lengthen").value = true;
else {

            if ($("#Length").data(kendoTextBox) == "") {
                $("#Length").data(kendoTextBox).value("0");
            }
            else {
                $("#Length").data(kendoTextBox).value();
            }
            $("#Length").data(kendoTextBox).enable(false);

However, I just want the value of the textbox to equal 0 if no value is entered or the textbox is blank. How can I adjust the following above?

但是,如果没有输入值或文本框为空,我只希望文本框的值等于 0。如何调整以上内容?

EDIT:

编辑:

How can I set the value of textbox to 0 if blank or empty?

如果空白或为空,如何将文本框的值设置为 0?

That is I have a simple textbox, if the user enters no value inside the textbox or the user leaves the textbox blank and goes somewhere else, I want it to automatically default back to 0 and not blank! I never want the textbox to be blank but contain some value whether it's 0 or any other integer value but never blank.

那就是我有一个简单的文本框,如果用户在文本框中没有输入任何值或者用户将文本框留空并转到其他地方,我希望它自动默认回 0 而不是空白!我从不希望文本框为空,但包含一些值,无论是 0 还是任何其他整数值,但从不为空。

Hope this helps!

希望这可以帮助!

回答by low_rents

update 3:if you consider spaces inside the textbox as "blank" as well:

更新 3:如果您将文本框内的空格也视为“空白”:

$("#Length").on("blur", function () {
    if ($(this).val().trim().length == 0) {
        $(this).val("0");
    }
});
//trigger blur once for the initial setup:
$("#Length").trigger("blur");

JSFiddle: http://jsfiddle.net/northkildonan/n1dd2opx/3/

JSFiddle:http: //jsfiddle.net/northkildonan/n1dd2opx/3/

update 2:if you want the textbox to be 0when it's left blank at all times:

更新 2:如果您希望文本框始终为0空白:

$("#Length").on("blur", function () {
    if ($(this).val().length == 0) {
        $(this).val("0");
    }
});
//trigger blur once for the initial setup:
$("#Length").trigger("blur");

JSFiddle: http://jsfiddle.net/northkildonan/n1dd2opx/1/

JSFiddle:http: //jsfiddle.net/northkildonan/n1dd2opx/1/

update:if you want to check for the length of a textbox with the id "Length", then it's:

更新:如果要检查 ID 为“Length”的文本框的长度,则为:

if ($("#Length").val().length == 0) {
    $("#Length").val("0");
};

JSFiddle: http://jsfiddle.net/northkildonan/n1dd2opx/

JSFiddle:http: //jsfiddle.net/northkildonan/n1dd2opx/

回答by user254153

Just five small lines of code:

只需五行代码:

$("#buttonid").on('click', function() {
   if ($("#textboxid").val() == "") {
      $("#textboxid").val("0");
   }
});

回答by Diana

$(document).ready(function(){ $("#amount").on("blur", function () {
if ($(this).val().trim().length == 0) {
    $(this).val("0");
}});});