<html>
<head>
<script src='https://code.jquery.com/jquery-2.1.3.min.js'></script>
</head>
<body>
<form action='insert.php' method='post' id='myform' >
<p>
<input type='text' name='fromno' placeholder='fromno' id='fromno' /><input type='text' name='tonum' placeholder='tonum' id='tonum' />
</p>
<button id='insert'>Insert</button>
<p id='result'></p>
<script src='insert.js'></script>
</form>
</body>
</html>
如果tonum
更重要,那么如何检查jQuery,然后fromno
如果tonum
低于fromno
,则应显示错误。
答案 0 :(得分:0)
检查内联评论以获取详细说明
$('#insert').on('click', function() {
var fromnum = $("#fromno").val();//get fromnum value
var tonum = $("#tonum").val();//get tonum value
if (fromnum != "" && tonum != "") {//check if both are not empty
if (parseInt(tonum) > parseInt(fromnum)) {//check if tonum is greater than fromnum
$("#result").text('Correct').addClass('valid');
//display result or continue with other code
} else {
$("#result").text('To num should be greater than From num').removeClass('valid');
//display error
}
} else
$("#result").text('Both fields has to be filled').removeClass('valid');
//display error if either of the field is empty
});
&#13;
/*Just for UI purpose*/
#result {
color: red;
}
.valid {
color: green !important;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action='insert.php' method='post' id='myform'>
<p>
<input type='text' name='fromno' placeholder='fromno' id='fromno' />
<input type='text' name='tonum' placeholder='tonum' id='tonum' />
</p>
<button id='insert'>Insert</button>
<p id='result'></p>
<script src='insert.js'></script>
</form>
&#13;