简体   繁体   中英

How to compare two input values onkey jquery

I have a form with two input fields.

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

I have tried this but no success

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

How to allow only second input values are only numbers and also less than the first input

You seem to be missing the key up event.

 $(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=""> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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