初始代码
var beginpoint=document.getElementById("beginp"); var endpoint=document.getElementById("end"); var patn = /^[-\+]?\d+$/i; if(from!='possible'){ if((beginpoint.value==''||endpoint.value=='')){ alert("起始点和结束点都不能为空并且为数字型"); return; }else if((!patn.test(beginpoint.value))){ alert("起始点和结束点都不能为空并且为数字型"); return; }else if(!patn.test(endpoint.value)){ alert("起始点和结束点都不能为空并且为数字型"); return; }else{ if(beginpoint.value>endpoint.value){ alert("起始点不能大于结束点"); return; } } }
以上代码发现例如:两个数4和12时,(beginpoint.value>endpoint.value)居然是判断为正确的。
总结:
很显然,document.getElementById("beginp").value取得的值是String类型的。所以要转换类型。需要这样写[color=red]if(Number(beginpoint.value)>Number(endpoint.value))[/color]