简体   繁体   English

将值与Java中的前导零进行比较

[英]Comparing values with leading zeros in Javascript

I have a javascript code that compares two values: 我有一个比较两个值的javascript代码:

} else if (!(parseInt($('#form_value').val()) >= 1)){
    alert("Error: You didn't pick a number!");

form_value in this case is 001, and I would like to compare it to the one, but it doesn't seem to work. 在这种情况下, form_value是001,我想将它与一个进行比较,但是它似乎不起作用。 I have tried using parseInt but it didn't work either. 我试过使用parseInt,但也没有用。 Any solutions? 有什么办法吗?

Try: 尝试:

if (!(Number(parseInt($('#form_value').val(),10)) >= 1)){

EDIT: try this shortened version: 编辑:尝试此缩短的版本:

if ( parseInt($('#form_value').val(),10) < 1){

Well, Number("001"); 好吧, Number("001"); returns 1 and Number("000"); 返回1和Number("000"); returns 0 返回0

based on your comment above 根据您上面的评论

"I'm trying to display an error if the value is less than 1, the lowest value a user can submit is 000 (which is loaded by default), if you pick something, it becomes 001." “如果值小于1,我正在尝试显示错误,用户可以提交的最小值是000(默认情况下已加载),如果您选择某些内容,它将变为001。”

If the lowest possible value is 0 then just test for 0... 如果最低可能值为0,则仅测试0 ...

var thing = Number($('#form_Value').val());
if (isNaN(thing) || thing === 0) {
  alert('an error message')'
}

可能是您应该将条件更改为if ( +( $('#form_value').val() ) < 1 )if (!+$('#form_value').val())

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

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