当我用HTML提交表单时,我正试图在Javascript中进行一些控制。
除了需要检查2个字段之和的情况外,Evertyhing工作得很好。
<script type="text/javascript">
function validation()
{
var count=0;
if((document.getElementsByName("people")[0].selectedIndex) == "")
{alert("How many PEOPLE?"); count++;}
else if((document.getElementsByName("amount")[0].selectedIndex) == "")
{alert("AMOUNT has to be at least 1."); count++;}
else if((document.getElementsByName("deposit")[0].value) == "")
{alert("DEPOSIT cannot be empty."); count++;}
else if((document.getElementsByName("deposit")[0].value) < "20")
{alert("DEPOSIT must be at least 20."); count++;}
else if((document.getElementsByName("topay")[0].value) == "")
{alert("Mark 0 in TO PAY if they dont have to pay more."); count++;}
else if( ( (document.getElementsByName("deposit")[0].value) + (document.getElementsByName("topay")[0].value) ) < "40")
{alert("The minimum price of this product is 40"); count++;}
else if((document.getElementsByName("seller")[0].value) == "")
{alert("Who is the SELLER?"); count++;}
else if(isNaN(document.getElementsByName("topay")[0].value))
{alert("TO PAY must be a number"); count++;}
else if(isNaN(document.getElementsByName("people")[0].value))
{alert("PEOPLE must be a number"); count++;}
else if(isNaN(document.getElementsByName("deposit")[0].value))
{alert("DEPOSIT must be a number"); count++;}
else if(isNaN(document.getElementsByName("amount")[0].value))
{alert("AMOUNT must be a number"); count++;}
return (count==0);
}
</script>
我无法解决的问题是:
else if( (((document.getElementsByName("deposit")[0].value) + (document.getElementsByName("topay")[0].value)) < "40)")
{alert("The minimum price of this product is 40"); count++;}
我希望它检查(存款+ topay)&lt; 40.我注意到这个代码只是检查(存款&lt; 40)并且我不明白为什么。
你能帮帮我吗?
答案 0 :(得分:1)
'+'运算符在条件语句中充当连接运算符(或通常执行连接)。
因此,为了使其执行加法操作,
else if( ((parseInt(document.getElementsByName("deposit")[0].value) + parseInt(document.getElementsByName("topay")[0].value)) < 40))
{alert("The minimum price of this product is 40"); count++;}
答案 1 :(得分:1)
var abc = parseInt( (document.getElementsByName(&#34;存&#34;)[0]。价值)+(document.getElementsByName(&#34; topay&#34;)[0]。价值) )if(abc&lt;&#34; 40&#34;){alert(&#34; h&#34; + abc); }
答案 2 :(得分:0)
将您的if条件更改为此
if( (((document.getElementsByName("deposit")[0].value) + (document.getElementsByName("topay")[0].value)) < 40))