JavaScript
function AdultNumber(){ /* Ticket Number Validation */
var AdInput = parseInt($("#adult").val()); //Declare it as an Integer
var ChildInput = parseInt ($("#child").val());
if (ChildInput >=0 && ChildInput <= 50 && AdInput > 0 && AdInput <= 50 || AdInput > 0 && AdInput <= 50 && !ChildInput ){ //If passengers are between 0 to 10
document.getElementById("AdTickError").textContent = "";
document.getElementById("ChildTickError").textContent = "";
return true;
}
else if( AdInput >= 10 ) { //If user input is greater than 10
confirm("Are you sure you want 10+ Passengers?");
document.getElementById("AdTickError").textContent = "";
return true;
}
else{
document.getElementById("AdTickError").textContent = "Incorrect Enter Correct Number Of Passengers";
return false;
}
}
HTML:
<div class="main-wrapper">
<Form action="#" class="group" name="form2" id="Form-2" method="post">
<legend id="fixed"><span class="number">2</span>Tickets</legend>
<div class="half-col">
<label for="Adult-ticket" class="center-label">Adults(+16)</label>
<input type="number" id="adult" name="user_adult">
<label id="AdTickError"></label>
</div>
<div class="half-col">
<label for="child-ticket" class="center-label">Child</label>
<input type="number" id="child" name="user_child">
<label id="ChildTickError"></label>
</div>
<button type = "button" value="checkout" id="checkoutbtn" onclick="AdultNumber(); calculateFare(); " >CHECKOUT</button>
为数字类型输入字段(子)和(成人)ID选择+ 10后,为什么没有显示确认框?如果两个框选择+10,则应显示两次。目前没有任何反应如果我选择提醒箱或确认箱,那会更合适吗?
答案 0 :(得分:0)
你的if语句的ChildInput和AdInput的最大值为50而不是10.它永远不会出现显示警告框的else if语句。
if (ChildInput >=0 && ChildInput <= 50 && AdInput > 0 && AdInput <= 50 || AdInput > 0 && AdInput <= 50 && !ChildInput ){ //If passengers are between 0 to 10
document.getElementById("AdTickError").textContent = "";
答案 1 :(得分:0)
试试这个,使用JQuery会更容易:
$('#checkoutbtn').on('click',function(){
var AdInput = parseInt($("#adult").val()); //Declare it as an Integer
var ChildInput = parseInt ($("#child").val());
if (ChildInput >=0 && ChildInput <= 50 && AdInput > 0 && AdInput <= 50 || AdInput > 0 && AdInput <= 50 && !ChildInput ){ //If passengers are between 0 to 10
document.getElementById("AdTickError").textContent = "";
document.getElementById("ChildTickError").textContent = "";
return true;
}
else if( AdInput >= 10 || ChildInput>=10) { //If user input is greater than 10
confirm("Are you sure you want 10+ Passengers?");
document.getElementById("AdTickError").textContent = "";
return true;
}
else{
document.getElementById("AdTickError").textContent = "Incorrect Enter Correct Number Of Passengers";
return false;
}
});
当然,请删除HTML按钮标记上的&#34; onclick&#34; 操作。