Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript最小值和最大值_Javascript_Jquery - Fatal编程技术网

Javascript最小值和最大值

Javascript最小值和最大值,javascript,jquery,Javascript,Jquery,我使用以下命令来限制文本字段中的值。只有当用户试图删除并留空时,它才不会允许删除,这才是非常有效的。它不会删除最小值。 数量: <input type="text" placeholder="none" name="notepad_and_pen" id="notepad_and_pen" maxlength="10" style="width:50px" tabindex=4 onchange="this.value = (this.value > 999999) ? 999999

我使用以下命令来限制文本字段中的值。只有当用户试图删除并留空时,它才不会允许删除,这才是非常有效的。它不会删除最小值。 数量:

<input type="text" placeholder="none" name="notepad_and_pen" id="notepad_and_pen" maxlength="10" style="width:50px" tabindex=4 onchange="this.value = (this.value > 999999) ? 999999 : ((this.value < 400) ? 400 : this.value);">
请需要帮助。我是一个彻头彻尾的傻瓜,你能教的任何东西都会很棒。多亏了大家。

这个值是一个字符串。你把它和一个数字比较。在比较之前将其转换为数字,如下所示:

parseFloat(this.value)
当然,如果您不使用内联事件处理程序,那么调试和使用它会容易得多。您已经在使用jQuery,请尝试以下操作:

$("#notepad_and_pen").on("change", function () {
    var $this = $(this),
        finalValue = $this.val(),
        myValue = parseFloat(finalValue);
    if (isNaN(myValue)) {
        finalValue = "";
    } else {
        if (myValue > 999999) {
            finalValue = 999999;
        } else if (myValue <= 0) {
            finalValue = "";
        } else if (myValue < 400) {
            finalValue = 400;
        }
    }
    $this.val(finalValue);
});
演示:

可以对记事本和笔进行限制,并允许使用一行代码删除

提供了一种检测占位符是否本机支持的方法,从而避免了对Modernizer的需要

并提供了一种非本地处理占位符的合理方法

以下是组合代码:

$(function() {
    //Impose limits on #notepad_and_pen
    $("#notepad_and_pen").on('change', function() {
        this.value = (this.value === '') ? '' : Math.max(400, Math.min(999999, Number(this.value)));
        //Use the following line to reject anything that's zero, blank or not a number, before applying the range limits.
        //this.value = (!Number(this.value)) ? '' : Math.max(400, Math.min(999999, Number(this.value)));
    });

    //With reference to http://diveintohtml5.info/detect.html
    function supports_input_placeholder() {
      var i = document.createElement('input');
      return 'placeholder' in i;
    }

    //With reference to http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html
    // 1. Handle placeholders (in non-HTML5 compliant browsers).
    if(!supports_input_placeholder()) {
        $('[placeholder]').focus(function () {
            var input = $(this);
            if (input.val() == input.attr('placeholder')) {
                input.val('');
                input.removeClass('placeholder');
            }
        }).blur(function () {
            var input = $(this);
            if (input.val() == '' || input.val() == input.attr('placeholder')) {
                input.addClass('placeholder');
                input.val(input.attr('placeholder'));
            }
        }).blur();

        //2. Prevent placeholder values being submitted to form's action script
        $('[placeholder]').eq(0).closest('form').submit(function () {
            $(this).find('[placeholder]').each(function () {
                var input = $(this);
                if (input.val() == input.attr('placeholder')) {
                    input.val('');
                }
            });
        });
    }
});

你想让它做什么。您的体验正是脚本所做的。请执行初始检查,然后绑定onChange eventListener。你应该做这个把戏,嗯?我不明白。求你了,我是30号的noob。我希望它们是一个强制数字,数量在下限和上限之间。但是,当用户决定返回并删除条目时,它将返回到一个带有占位符的空白文本框。这正接近我要查找的内容。我只想输入介于400和999999之间的范围号。但若用户返回并删除数字,那个么它将返回为空,并带有那个里的占位符。@user2292469我很困惑。它就是这样做的。如果你把东西放在400以下,它就是400。如果你放的东西高于9999999,它就是9999999。如果将其留空或输入无效数字,则会将占位符输入。你刚才不是这么说的吗?或者你是说如果你把它留空,你希望它是400,而不是占位符?它工作得很好!如果可以,您如何添加功能,如果用户键入0,它将自动变为空白并进入占位符?@user2292469在第一个If之后添加一个else If myValue==0{finalValue=;}。@user2292469抱歉,我刚刚回去测试,发现它没有完全工作。我用新的代码和新的JSFIDLE更新了我的答案。我认为应该完成一些事情。如果为0或更小,则使用占位符。如果小于400,则使用400。如果大于9999999,则使用9999999。如果一开始它不是一个数字,它就使用占位符。试过这个。它与原始的内联操作相同。当我输入一个数字,它检测到下限和上限之间,它是ok的,如果它低于下限,它就会强制下限。然而,当我尝试返回并删除条目时,它会强制下限,而不是删除并保留空格。哎哟,抱歉,我在简化射程检查时挂断了电话,忘了回答这个问题。答:这也很好用!如果可以,您如何添加功能,如果用户键入0,它将自动变为空白并添加到占位符中?我在另一行中进行了编辑,该行将拒绝任何零、空白或非数字的内容。这应该能满足大多数甚至所有的可能性。老兄,你太棒了。非常感谢
$(function() {
    //Impose limits on #notepad_and_pen
    $("#notepad_and_pen").on('change', function() {
        this.value = (this.value === '') ? '' : Math.max(400, Math.min(999999, Number(this.value)));
        //Use the following line to reject anything that's zero, blank or not a number, before applying the range limits.
        //this.value = (!Number(this.value)) ? '' : Math.max(400, Math.min(999999, Number(this.value)));
    });

    //With reference to http://diveintohtml5.info/detect.html
    function supports_input_placeholder() {
      var i = document.createElement('input');
      return 'placeholder' in i;
    }

    //With reference to http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html
    // 1. Handle placeholders (in non-HTML5 compliant browsers).
    if(!supports_input_placeholder()) {
        $('[placeholder]').focus(function () {
            var input = $(this);
            if (input.val() == input.attr('placeholder')) {
                input.val('');
                input.removeClass('placeholder');
            }
        }).blur(function () {
            var input = $(this);
            if (input.val() == '' || input.val() == input.attr('placeholder')) {
                input.addClass('placeholder');
                input.val(input.attr('placeholder'));
            }
        }).blur();

        //2. Prevent placeholder values being submitted to form's action script
        $('[placeholder]').eq(0).closest('form').submit(function () {
            $(this).find('[placeholder]').each(function () {
                var input = $(this);
                if (input.val() == input.attr('placeholder')) {
                    input.val('');
                }
            });
        });
    }
});