javascript表单提交一个值应该大于其他值x> y

时间:2013-11-25 08:52:22

标签: javascript

我的代码无法正常使用

时:

function verify() {
 var x = document.add_form.max_temp.value ;
 var y = document.add_form.min_temp.value == "";
 if (x < y) {
  alert("Sorry, you don't have enough points");return false;
 }
}

HTML:

<form id="add_form" action="add_mor_data.php" method="post">
   <input type="text" id="max_temp" name="max_temp" />
   <input type="text" id="min_temp" name="min_temp" />
   <input type="submit" value="Submit" class="search" onClick="return (verify());"/>
</form>

1 个答案:

答案 0 :(得分:1)

您比较字符串而不是数字,并且必须将其转换为整数。

将您的功能更改为:

verify = function() {
     var x = document.getElementById("max_temp").value;
     var y = document.getElementById("min_temp").value;
     if (parseInt(x) < parseInt(y)) {
      alert("Sorry, you don't have enough points");
      return false;
     }
    }

这是一个有效的Fiddle