jQuery Jquery如何检查输入的数字是否大于99

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5704957/
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-08-26 19:40:40  来源:igfitidea点击:

Jquery how to check if a input has a number higher then 99

jquery

提问by Lawrence

Was wanting to know how to check using jquery if a input contains a number higher then 99

想知道如何使用 jquery 检查输入是否包含大于 99 的数字

回答by Naftali aka Neal

try this:

尝试这个:

if(parseInt($("selector").val()) > 99) {
    /*if it is*/
}

or if you are checking it on change:

或者如果您正在检查更改:

$("selector").change(function(){
     if(parseInt(this.value) > 99){
        /*if it is*/
     } 
})

Editas said in the below comments, it might be better to use parseFloatinstead of parseIntto compare the numbers

如以下评论中所述进行编辑,最好使用parseFloat而不是parseInt比较数字

回答by Gerben Jacobs

if ($("#myfield").val() > 99) {

}

回答by Damb

if(parseInt($(YOURINPUT).val()) > 99) // do something

回答by Sang Suantak

If by input you mean text, then use the following:

如果输入是指文本,则使用以下内容:

if (parseFloat($("#inputid").val()) > 99) {
   //do something
}

回答by ryryan

Simply use this:

只需使用这个:

if ($("input.INPUTNAME").val() > 99) {
//above 99 code
}

You may want to have an else command, if the input is below 99.

如果输入低于 99,您可能需要一个 else 命令。

if ($("input.INPUTNAME").val() > 99) {
    //above 99 code.
    }
else{
     //Not above 99 code.
}

回答by Nikhil

This would do it

这会做到

var value = $("input selector").eq(0).val();
if(isNaN(value)){
 //do stuff
}

else{
var num = value - 0;
if(num>99)
{
 //value is greater than 99
}

}

回答by Galled

Use this:

用这个:

html:

html:

<input type="text" id="myInput" />

jquery:

查询:

var nMyInput = $("#myInput").val();
if (typeof nMyInput != "undefined"){
    if(!isNaN(nMyInput)){
        if (parseInt(nMyInput) > 99) {
            /* do your stuff */
        }
    }
}

回答by Marc Bouvier

I guess typically with jQuery you would have to have the name of the form

我想通常使用 jQuery 你必须有表单的名称

<form id="myForm" action="comment.php" method="post"> 
    number: <input id="form_number" type="text" name="number" /> 
    Comment: <textarea name="comment"></textarea> 
    <input type="submit" value="Submit Comment" /> 
</form>

you would select the form and its input.

您将选择表单及其输入。

var myInt = parseInt(jQuery("#form-number-url").attr("value"));

then you can compare it with any other integer.

然后你可以将它与任何其他整数进行比较。