简体   繁体   English

如果javascript中的条件超过未读的值

[英]value more than not read in if condition in javascript

I m getting two textboxes value as 5 and 10. 我得到两个文本框,分别为5和10。

so im validating them on the following if condition 所以我在以下情况下对它们进行验证

var textboxvalue1 = $('#textboxvalue1' + counter).val();
var textboxvalue2 = $('#textboxvalue2' + counter).val();
if (textboxvalue1 < textboxvalue2) {
    alert("error");
}
textboxvalue1 = 10
textboxvalue2 = 5

its showing an alert in this case.which it shud nt show.bt when textboxvalue1 is less than 10,it works fine. 在这种情况下,它显示警报。当textboxvalue1小于10时,它会关闭nt show.bt,它可以正常工作。

Actually your .val() returns string you try to convert it as integer so use parseInt() in your context and check. 实际上,您的.val()返回字符串,您尝试将其转换为整数,因此请在上下文中使用parseInt()并进行检查。

The parseInt() function parses a string and returns an integer. parseInt()函数解析一个字符串并返回一个整数。

Note : 注意事项

The radix parameter is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number. 基数参数用于指定要使用的数字系统,例如,基数16(十六进制)表示应将字符串中的数字从十六进制数字解析为十进制数字。

If the radix parameter is omitted, JavaScript assumes the following: 如果省略了radix参数,则JavaScript假定以下内容:

If the string begins with "0x", the radix is 16 (hexadecimal) If the string begins with "0", the radix is 8 (octal). 如果字符串以“ 0x”开头,则基数为16(十六进制)如果字符串以“ 0”开头,则基数为8(八进制)。 This feature is deprecated If the string begins with any other value, the radix is 10 (decimal) 不建议使用此功能,如果字符串以任何其他值开头,则基数为10(十进制)

var textboxvalue1= parseInt($('#textboxvalue1'+counter).val(), 10);
var textboxvalue2= parseInt($('#textboxvalue2'+counter).val(), 10);

if (textboxvalue1 < textboxvalue2) {
    alert("error");
}

You have to convert your input strings to numbers like this: 您必须将输入字符串转换为数字,如下所示:

var textboxvalue1=parseInt($('#textboxvalue1'+counter).val());
var textboxvalue2=parseInt($('#textboxvalue2'+counter).val());

The values are being interpreted as strings by JavaScript, not numbers. JavaScript将值解释为字符串,而不是数字。 Wrap them in parseInt or parseFloat before comparing them. 比较它们之前,将它们包装在parseIntparseFloat

Use ParseInt Because var return string default. 使用ParseInt因为var返回默认字符串。 Or you can use parseFloat 或者您可以使用parseFloat

var textboxvalue1= parseInt($('#textboxvalue1'+counter).val(), 10);
var textboxvalue2= parseInt($('#textboxvalue2'+counter).val(), 10);
if (textboxvalue1 < textboxvalue2) {
    alert("error");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM