Jquery如何检查输入的数字是否高于99
想知道如何使用jquery检查输入中是否包含大于99的数字请尝试以下操作:Jquery如何检查输入的数字是否高于99,jquery,Jquery,想知道如何使用jquery检查输入中是否包含大于99的数字请尝试以下操作: if ($("#myfield").val() > 99) { } if(parseInt($("selector").val()) > 99) { /*if it is*/ } 或者,如果您正在检查更改: $("selector").change(function(){ if(parseInt(this.value) > 99){ /*if it is*/
if ($("#myfield").val() > 99) {
}
if(parseInt($("selector").val()) > 99) {
/*if it is*/
}
或者,如果您正在检查更改:
$("selector").change(function(){
if(parseInt(this.value) > 99){
/*if it is*/
}
})
编辑如以下评论所述,最好使用parseFloat
而不是parseInt
来比较数字这样就可以了
if(parseInt($(YOURINPUT).val()) > 99) // do something
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
}
}
如果“输入”指的是文本,则使用以下命令:
if (parseFloat($("#inputid").val()) > 99) {
//do something
}
简单地说:
if ($("input.INPUTNAME").val() > 99) {
//above 99 code
}
如果输入低于99,则可能需要使用else命令
if ($("input.INPUTNAME").val() > 99) {
//above 99 code.
}
else{
//Not above 99 code.
}
使用以下命令:
html:
我想通常使用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>
然后您可以将其与任何其他整数进行比较。@Lawrence i添加了一个选项,如果您在输入时检查它change@Lawrence,我猜由于堆栈限制,这不适用于大约5分钟^ ^是的,这是真的,它不会让我多说几句minutes@Lawrence现在时间限制应该设置为^ ^您不需要parsefloat或parseint:('99'==99)=true。另外:((“100”>99)=true
num=值-0是什么代码>用于?这是将数字的字符串表示形式转换为真数字。这将把“99”字符串文字转换成99个数字。有多种方法可以做到这一点(parseInt、parseFloat等)。这是我使用的一种方法。哇,你真的不需要所有的if语句parseInt
应该为您完成所有这些工作我不知道parseInt完成了所有工作(检测是否未定义,文本是否不是数字)^ ^这与jQuery无关。在JavaScript('100'==100)=true中,您不需要parsefloat或parseint:('99'==99)=true。另外:('100“>99)=true您不需要parsefloat或parseint:('99'==99)=true。另外:(“100”>99)=true@herostwist:我已经遇到了这样的情况,没有parseInt转换就不能正常工作,所以我喜欢从那时起安全地进行转换。我不记得上下文,所以不要问:)
var nMyInput = $("#myInput").val();
if (typeof nMyInput != "undefined"){
if(!isNaN(nMyInput)){
if (parseInt(nMyInput) > 99) {
/* do your stuff */
}
}
}
<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>
var myInt = parseInt(jQuery("#form-number-url").attr("value"));