gpt4 book ai didi

javascript - 从javascript中的输入框控制总和

转载 作者:行者123 更新时间:2023-11-29 21:48:26 26 4
gpt4 key购买 nike

当我提交 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),我不明白为什么。

你能帮帮我吗?

最佳答案

“+”运算符在条件语句中充当连接运算符(或通常执行连接)。

因此要让它执行加法运算使用,

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++;}

关于javascript - 从javascript中的输入框控制总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30257083/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com