javascript 中输入框的控制总和
Control sum from input boxes in javascript
当我在 HTML 中提交表单时,我试图在 Javascript 中进行一些控制。
除需要检查 2 个字段之和的那个以外,其他一切都完美无缺。
<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++;}
我想让它检查 if (deposit + topay) < 40。我注意到这段代码只是检查 if (deposit < 40),我不明白为什么。
你能帮帮我吗?
将您的 if 条件更改为此
if( (((document.getElementsByName("deposit")[0].value) + (document.getElementsByName("topay")[0].value)) < 40))
'+'运算符在条件语句中充当连接运算符(或通常执行连接)。
因此要让它执行加法运算使用,
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++;}
var abc = parseInt(
(document.getElementsByName("deposit")[0].value)+(document.getElementsByName("topay")[0].value)
) if( abc < "40" ) { alert("h"+abc); }
当我在 HTML 中提交表单时,我试图在 Javascript 中进行一些控制。
除需要检查 2 个字段之和的那个以外,其他一切都完美无缺。
<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++;}
我想让它检查 if (deposit + topay) < 40。我注意到这段代码只是检查 if (deposit < 40),我不明白为什么。
你能帮帮我吗?
将您的 if 条件更改为此
if( (((document.getElementsByName("deposit")[0].value) + (document.getElementsByName("topay")[0].value)) < 40))
'+'运算符在条件语句中充当连接运算符(或通常执行连接)。
因此要让它执行加法运算使用,
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++;}
var abc = parseInt( (document.getElementsByName("deposit")[0].value)+(document.getElementsByName("topay")[0].value) ) if( abc < "40" ) { alert("h"+abc); }