javascript jQuery.val() 整数数据类型比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27347004/
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
jQuery.val() integer datatype comparison
提问by Foster
I am bit confused of this integer comparison using jQuery
我对使用 jQuery 的这个整数比较有点困惑
I have 2 input values.
我有 2 个输入值。
- input type="number" class="highestBid" value=
- input type="number" min="0" class=" bid" name="bid" id="bid"
- input type="number" class="highestBid" value=
- input type="number" min="0" class="bid" name="bid" id="bid"
so basically the inputed bid from user in the 2nd input field must be always higher than the current highest bid.
所以基本上用户在第二个输入字段中输入的出价必须始终高于当前的最高出价。
I used following jQuery code to compare between these 2 codes :
我使用以下 jQuery 代码来比较这两个代码:
$(function(){
$('.bid').focusout(function(){
if($('.bid').val() <= $('.highestBid').val()){
$('.bidPlaced').prop('disabled', true);
}
else{
$('.bidPlaced').prop('disabled', false);
$('.buyItNow').prop('disabled', false)
}
});
});
});
The code works fine, but just now there is a bug I figured out
代码工作正常,但刚才我发现了一个错误
if the current highest bid is : $123 and the user input : $21 the function will not work
如果当前最高出价为:123 美元且用户输入:21 美元,则该功能将不起作用
but if the user key in $122, the code will work
但如果用户输入 122 美元,代码将起作用
I found out that the jQuery code just compare the first value which in this case is $(1)23 to $(2)1.
我发现 jQuery 代码只是比较第一个值,在这种情况下是 $(1)23 和 $(2)1。
can anybody tell me what is wrong with the jQuery code?
谁能告诉我 jQuery 代码有什么问题?
Thanks!
谢谢!
回答by danludwig
When you get a value our of an input
element using jQuery .val()
, it will always be a string
. It doesn't matter that the input
element is type="number"
. The javascriptvalue is a string
.
当您input
使用 jQuery获取元素的值 our 时.val()
,它将始终是string
. 不要紧的input
元素type="number"
。在JavaScript的值是一个string
。
If you know your numbers will always be integers, you can parseInt
:
如果你知道你的数字总是整数,你可以parseInt
:
if (parseInt($('.bid').val()) <= parseInt($('.highestBid').val())) {
If your numbers possibly have decimal places, you can also use parseFloat
instead of parseInt
.
如果您的数字可能有小数位,您也可以使用parseFloat
代替parseInt
。
if (parseFloat($('.bid').val()) <= parseFloat($('.highestBid').val())) {
BTW, it is not jQuery that is behaving this way, it is the javascript language. All jQuery does is use javascript to read out the value of the input element in a convenient way, and give it back to you as a string.
顺便说一句,这种行为的不是 jQuery,而是 javascript 语言。jQuery 所做的就是使用 javascript 以方便的方式读出输入元素的值,并将其作为字符串返回给您。
It's up to you to know that these values are always string
s, and if you need to compare them as number
s, use javascript functions like parseInt
and parseFloat
to convert them into the correct javascript types before comparing.
您需要知道这些值始终为string
s,如果您需要将它们作为number
s进行比较,请在比较之前使用 javascript 函数parseInt
和parseFloat
将它们转换为正确的 javascript 类型。