繁体   English   中英

如何比较两个输入值onkey jQuery

[英]How to compare two input values onkey jquery

我有一个带有两个输入字段的表单。

<input type="text" id="first" value="">
<input type="text" id="second" value="">

我已经尝试过了但是没有成功

<script>
$(function(){
    var fst=$("#first").val();
    var sec=$("#second").val();
    if (sec>fst) {
        alert("Second value should less than first value");
        return true;
    }
})
</script>

如何仅允许第二个输入值是数字并且还小于第一个输入

您似乎缺少关键事件。

 $(function(){ $("#first, #second").on("keyup", function () { var fst=$("#first").val(); var sec=$("#second").val(); if (Number(sec)>Number(fst)) { alert("Second value should less than first value"); return true; } }) }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="first" value=""> <input type="text" id="second" value=""> 

暂无
暂无

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

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