Javascript 将变量(文本框)乘以一个数字,如果满足某些条件,则根据文本框中的输入发送警报
我编写了javascript代码,用于在文本框中满足某些条件时提醒用户。如果满足这些条件,变量“singlenum”应乘以40,并成为网页(当前未)发送的警报的一部分Javascript 将变量(文本框)乘以一个数字,如果满足某些条件,则根据文本框中的输入发送警报,javascript,jquery,html,Javascript,Jquery,Html,我编写了javascript代码,用于在文本框中满足某些条件时提醒用户。如果满足这些条件,变量“singlenum”应乘以40,并成为网页(当前未)发送的警报的一部分 <!doctype html> <html> <head> <meta charset="utf-8"> <title>West London Boats</title> <meta name="description" content="A
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>West London Boats</title>
<meta name="description" content="A company based in West London, offering high quality boat voyages down the Thames">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/style.css">
</head>
<script type="text/javascript">
function weclicked() {
var singlenum = document.getElementById("singlenum").value;
var tickettype = document.getElementById("tickettype").value;
var areanum = document.getElementById("areanum").value;
if (tickettype == "adult" && areanum == 1) {
(singlenum * 40) {
alert ("Your booking has been placed, you should recieve an email with your details shortly. Your booking is: " + singlenum + " adult ticket(s) to Area " + areanum + ". This will cost: " + singlenum * 40 + ".");
}
}
}
</script>
<body>
<section class="book" id="book">
<div><p><br></p>
<div class="container group">
<h3>Book</h3>
<form>
<fieldset class="group">
<div class="column form-left">
<input type="text" name="fullname" placeholder="Name">
<input type="email" name="email" placeholder="email@address.com">
</div>
<div class="column form-left">
<input type="value" id="singlenum" placeholder="Number of single tickets"/>
</div>
<div class="column form-left">
<input type="value" id="areanum" placeholder="Area number"/>
</div>
<div class="column form-left">
<input type="value" id="tickettype" placeholder="Adult, child or concession"/>
</div>
<textarea name="message" placeholder="Any queries?"></textarea>
</fieldset>
<div class="button">
<button type="button" onClick="weclicked()" id="button">Book</button>
</div>
</form>
</div>
</section>
</body>
</html>
西伦敦船只
函数weclicked(){
var singlenum=document.getElementById(“singlenum”).value;
var tickettype=document.getElementById(“tickettype”).value;
var areanum=document.getElementById(“areanum”).value;
如果(票证类型==“成人”&&areanum==1){
(单数*40){
提醒(“您的预订已经完成,您应该很快就会收到一封包含详细信息的电子邮件。您的预订是:“+singlenum+”到“+areanum+”区域的成人票。这将花费:“+singlenum*40+”);
}
}
}
书
书
我不知道为什么什么都没发生,在过去的两天里,我浏览了各种网站和问题,寻找答案,没有发现任何东西,我现在请求社区抽出一点时间来帮助我。你能告诉我如何修复我的代码,为什么它不工作,以及你是如何修复的吗?非常感谢。应该是这样的:
if (tickettype.toLowerCase() == "adult" && areanum == 1) {
singlenum *= 40; // check if it's correct for your logic
alert ("Your booking has been placed, you should recieve an email with your details shortly. Your booking is: " + singlenum + " adult ticket(s) to Area " + areanum + ". This will cost: " + singlenum * 40 + ".");
}
<script type="text/javascript">
function weclicked() {
var singlenum = document.getElementById("singlenum").value;
var tickettype = document.getElementById("tickettype").value;
var areanum = document.getElementById("areanum").value;
if (tickettype.toLowerCase() == "adult" && areanum == 1)
singlenum2 = singlenum * 40 ;
alert ("Your booking has been placed, you should recieve an email with your details shortly. Your booking is: " + singlenum + " adult ticket(s) to Area " + areanum + ". This will cost: " + singlenum2 + ".");
}
</script>
不确定是否要将singlenum
乘以40两次,由您决定
在if
条件中,将.toLowerCase()
添加到tickettype
,以覆盖所有“成人”、“成人”、“成人”等
试试这段代码。
只是做了一些简单的修改,对我来说它是有效的
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>West London Boats</title>
<meta name="description" content="A company based in West London, offering high quality boat voyages down the Thames">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/style.css">
</head>
<script type="text/javascript">
function weclicked() {
var singlenum = document.getElementById("singlenum").value;
var tickettype = document.getElementById("tickettype").value;
var areanum = document.getElementById("areanum").value;
if (tickettype == "adult" && areanum == 1)
singlenum= singlenum * 40 ;
alert ("Your booking has been placed, you should recieve an email with your details shortly. Your booking is: " + singlenum + " adult ticket(s) to Area " + areanum + ". This will cost: " + singlenum + ".");
}
</script>
<body>
<section class="book" id="book">
<div><p><br></p>
<div class="container group">
<h3>Book</h3>
<form>
<fieldset class="group">
<div class="column form-left">
<input type="text" name="fullname" placeholder="Name">
<input type="email" name="email" placeholder="email@address.com">
</div>
<div class="column form-left">
<input type="value" id="singlenum" placeholder="Number of single tickets"/>
</div>
<div class="column form-left">
<input type="value" id="areanum" placeholder="Area number"/>
</div>
<div class="column form-left">
<input type="value" id="tickettype" placeholder="Adult, child or concession"/>
</div>
<textarea name="message" placeholder="Any queries?"></textarea>
</fieldset>
<div class="button">
<button type="button" onClick="weclicked()" id="button">Book</button>
</div>
</form>
</div>
</section>
</body>
</html>
西伦敦船只
函数weclicked(){
var singlenum=document.getElementById(“singlenum”).value;
var tickettype=document.getElementById(“tickettype”).value;
var areanum=document.getElementById(“areanum”).value;
如果(票证类型==“成人”&&areanum==1)
singlenum=singlenum*40;
提醒(“您的预订已经完成,您应该很快就会收到一封包含详细信息的电子邮件。您的预订是:“+singlenum+”到“+areanum+”区域的成人票。这将花费:“+singlenum+”);
}
书
书
利用Nitin提供的改进,我能够想出一个解决方案。
javascript部分应如下所示:
if (tickettype.toLowerCase() == "adult" && areanum == 1) {
singlenum *= 40; // check if it's correct for your logic
alert ("Your booking has been placed, you should recieve an email with your details shortly. Your booking is: " + singlenum + " adult ticket(s) to Area " + areanum + ". This will cost: " + singlenum * 40 + ".");
}
<script type="text/javascript">
function weclicked() {
var singlenum = document.getElementById("singlenum").value;
var tickettype = document.getElementById("tickettype").value;
var areanum = document.getElementById("areanum").value;
if (tickettype.toLowerCase() == "adult" && areanum == 1)
singlenum2 = singlenum * 40 ;
alert ("Your booking has been placed, you should recieve an email with your details shortly. Your booking is: " + singlenum + " adult ticket(s) to Area " + areanum + ". This will cost: " + singlenum2 + ".");
}
</script>
函数weclicked(){
var singlenum=document.getElementById(“singlenum”).value;
var tickettype=document.getElementById(“tickettype”).value;
var areanum=document.getElementById(“areanum”).value;
if(tickettype.toLowerCase()=“成人”&&areanum==1)
singlenum2=singlenum*40;
提醒(“您的预订已经完成,您应该很快就会收到一封包含详细信息的电子邮件。您的预订是:“+singlenum+”到“+areanum+”区域的成人票。这将花费:“+singlenum2+”);
}
它使用新的术语“singlenum2”来保持方程“singlenum*40”。它允许用户查看已发行的单张票证的数量和成本,而无需使用等式来应用单张票证的数量
非常感谢所有响应我请求帮助的人,我真的很感激。祝你愉快 您在2015年10月29日解决的条件块的第一行中有语法错误。此代码将在每次用户根据要求订票时发出警报,并且您提到的特定条件(成人和1号区域的singlenum应乘以40)也可以正常工作。非常感谢!如何更改代码,以便警报输出第一个“singlenum”作为原始的非乘法数字?例如,如果要乘以40的数字是1,我如何使警报变为:您的预订是:1张到1区的成人票。这将花费:40。很高兴我能帮助你。谢谢你,但我试过了,仍然没有警报。当你尝试它时,它工作了吗?我查看了你的代码,结果是单张票的数量是40张(应该是1张),成本是1600张(应该是40张)。原因是单张票的数量应该是用户首先输入的数字,而不是等式的结果。第二部分是不正确的,因为你重复了这个等式,导致1乘以40,然后再乘以40(基本上是40^2)。要解决这个问题,您需要使等式只发生一次,并将术语更改为仅指在警报中第二次使用“singlenum”。