HTML:
<html>
<body>
<select class="FormInput" id="minheight" style="width: 120px;" name="minheight">
<option selected="selected" value="">- Select -</option>
<option value=1>4' 0" (1.22 mts)</option>
<option value=2>4' 1" (1.24 mts)</option>
<option value=3>4' 2" (1.28 mts)</option>
<option value=4>4' 3" (1.31 mts)</option>
<option value=5>4' 4" (1.34 mts)</option>
<option value=6>4' 5" (1.35 mts)</option>
<option value=7>4' 6" (1.37 mts)</option>
<option value=8>4' 7" (1.40 mts)</option>
<option value=9>4' 8" (1.42 mts)</option>
<option value=10>4' 9" (1.45 mts)</option>
<option value=11>4' 10" (1.47 mts)</option>
<option value=12>4' 11" (1.50 mts)</option>
<option value=13>5' 0" (1.52 mts)</option>
<option value=14>5' 1" (1.55 mts)</option>
<option value=15>5' 2" (1.58 mts)</option>
<option value=16>5' 3" (1.60 mts)</option>
<option value=17>5' 4" (1.63 mts)</option>
<option value=18>5' 5" (1.65 mts)</option>
<option value=19>5' 6" (1.68 mts)</option>
<option value=20>5' 7" (1.70 mts)</option>
<option value=21>5' 8" (1.73 mts)</option>
<option value=22>5' 9" (1.75 mts)</option>
<option value=23>5' 10" (1.78 mts)</option>
<option value=24>5' 11" (1.80 mts)</option>
<option value=25>6' 0" (1.83 mts)</option>
<option value=26>6' 1" (1.85 mts)</option>
<option value=27>6' 2" (1.88 mts)</option>
<option value=28>6' 3" (1.91 mts)</option>
<option value=29>6' 4" (1.93 mts)</option>
<option value=30>6' 5" (1.96 mts)</option>
<option value=31>6' 6" (1.98 mts)</option>
<option value=32>6' 7" (2.01 mts)</option>
<option value=33>6' 8" (2.03 mts)</option>
<option value=34>6' 9" (2.06 mts)</option>
<option value=35>6' 10" (2.08 mts)</option>
<option value=36>6' 11" (2.11 mts)</option>
<option value=37>7' (2.13 mts) plus</option>
</select> to
<select class="FormInput" id="maxheight" style="width: 120px;" name="maxheight">
<option selected="selected" value="">- Select -</option>
<option value=1>4' 0" (1.22 mts)</option>
<option value=2>4' 1" (1.24 mts)</option>
<option value=3>4' 2" (1.28 mts)</option>
<option value=4>4' 3" (1.31 mts)</option>
<option value=5>4' 4" (1.34 mts)</option>
<option value=6>4' 5" (1.35 mts)</option>
<option value=7>4' 6" (1.37 mts)</option>
<option value=8>4' 7" (1.40 mts)</option>
<option value=9>4' 8" (1.42 mts)</option>
<option value=10>4' 9" (1.45 mts)</option>
<option value=11>4' 10" (1.47 mts)</option>
<option value=12>4' 11" (1.50 mts)</option>
<option value=13>5' 0" (1.52 mts)</option>
<option value=14>5' 1" (1.55 mts)</option>
<option value=15>5' 2" (1.58 mts)</option>
<option value=16>5' 3" (1.60 mts)</option>
<option value=17>5' 4" (1.63 mts)</option>
<option value=18>5' 5" (1.65 mts)</option>
<option value=19>5' 6" (1.68 mts)</option>
<option value=20>5' 7" (1.70 mts)</option>
<option value=21>5' 8" (1.73 mts)</option>
<option value=22>5' 9" (1.75 mts)</option>
<option value=23>5' 10" (1.78 mts)</option>
<option value=24>5' 11" (1.80 mts)</option>
<option value=25>6' 0" (1.83 mts)</option>
<option value=26>6' 1" (1.85 mts)</option>
<option value=27>6' 2" (1.88 mts)</option>
<option value=28>6' 3" (1.91 mts)</option>
<option value=29>6' 4" (1.93 mts)</option>
<option value=30>6' 5" (1.96 mts)</option>
<option value=31>6' 6" (1.98 mts)</option>
<option value=32>6' 7" (2.01 mts)</option>
<option value=33>6' 8" (2.03 mts)</option>
<option value=34>6' 9" (2.06 mts)</option>
<option value=35>6' 10" (2.08 mts)</option>
<option value=36>6' 11" (2.11 mts)</option>
<option value=37>7' (2.13 mts) plus</option>
</select>
<span></span>
</body>
</html>
CSS:
.form-error { color:red; font-size:12px;}
.form-info { color:#B1B1B1; font-size:12px;}
.form-ok { color:green; font-size:12px; }
和脚本:
var minheight = $("#minheight");
var maxheight = $("#maxheight");
maxheight.blur(validatemaxheight);
$('#minheight,#maxheight').change(function(){
heightvalidate();
});
function validatemaxheight()
{
heightvalidate();
}
function heightvalidate()
{
console.log('minheight:: '+minheight.val()+' maxheight:: '+maxheight.val());
if(minheight.val() > maxheight.val())
{
minheight.parent().find('span').attr("class","form-error").html("Minimum height should not be greater than maximum height.");
minheight.focus();
return false;
}
else if(minheight.val() == maxheight.val())
{
minheight.parent().find('span').attr("class","form-error").html("Minimum height should be less than maximum height.");
minheight.focus();
return false;
}
else
{
minheight.parent().find('span').attr("class","form-ok").html('OK');
return true;
}
}
我实际上是在比较最小高度和最大高度。如果minheight&gt;最大高度然后,将显示错误消息。
上述条件正常,直到选择高于9且最大高度值大于9的最小高度值,大于最小高度。尽管minheight&lt; maxheight,仍然是错误消息显示minheight大于maxheight。
这里的实时版本: http://jsfiddle.net/prajan55/u9NJD/
任何帮助表示赞赏..
答案 0 :(得分:3)
在比较它们之前对这些值使用parseInt(),实际将它们比作数字而不是字符串。
答案 1 :(得分:1)
我认为这是因为检查是在字符串而不是整数上执行此操作,您必须先使用parseInt(minheight.val())&gt;将它们转换为整数。 parseInt函数(maxheight.val())