如何在Javascript中使用比较运算符作为两个输入值 [英] How to use comparison operator in Javascript for two input values
本文介绍了如何在Javascript中使用比较运算符作为两个输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想比较两个输入值,只是尝试使用javascript,但它没有正常工作。我正在使用以下代码
I want to compare the two input values, just try in javascript, but it's not working fine. I'm using the following code
function check_closing()
{
var opening = $('#opening').val();
var closing = $('#closing').val();
if(opening > closing)
{
alert('Opening is greater than Closing. Please enter the correct value');
$('#closing').val('');
}
}
如果开盘价输入= 8541,收盘价就像= 8241它工作正常,但如果收盘价为954则无法正常工作。请帮忙。
if the opening value input = 8541, closing value like = 8241 it's work fine, but if the closing is 954 it's not working. Please help.
提前致谢。
推荐答案
转换字符串比较之前的整数:
Convert the strings to integers before comparison:
function check_closing()
{
var opening = $('#opening').val();
var closing = $('#closing').val();
if(parseInt(opening) > parseInt(closing))
{
alert('Opening is greater than Closing. Please enter the correct value');
$('#closing').val('');
}
}
这篇关于如何在Javascript中使用比较运算符作为两个输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文