如果第一个表单未正确验证,我如何隐藏表单
How can I hide form if first form is not validated properly
JavaScript:
function myFunction() { //creating the onlick function
var locationFrom = document.getElementById("LocationFrom").value;// Getting both "from" and "to" Ids fromt he homepage.hmtl
var locationTo = document.getElementById("LocationTo").value;
if (locationFrom === locationTo) { // If both have same values then give an error as below
document.getElementById("errorLocationFrom").textContent = 'Destination cannot be the same.';
}
else {
document.getElementById("errorLocationFrom").textContent = ''; // if not the same then give no error
}
$("#Submit").click(function(){
$("#Form-2").show(); //Display form when Submit button is clicked
});
function SecForm(){
var ErrorLocation = document.getElementById("errorLocationFrom");
if(ErrorLocation == false) //Hide form2 if ErrorLocation is false
$("#Form-2").hide();
return false;
}
HTML:
<div class="full-col">
<label for="FDestination">From</label> <!---Label for select element--->
<select name="Location" id = "LocationFrom"> <!---Select element to give user options to select from-->
<option value="" disabled selected>Please Select </option> <!---Options for departing location-->
<option value="Newport">Newport</option>
<option value="Mahdi">London</option>
<option value="Cardiff">Cardiff</option>
<option value="Cilo">Brazil </option>
</select>
<label for="FDestination">To</label>
<select name="LocationTo" id = "LocationTo" >
<option value="" disabled selected>Please Select </option>
<option value="Cardiff">Cardiff</option>
<option value="Mahdi">London</option>
<option value="Newport">Newport</option>
<option value="Cilo">Brazil</option>
</select>
<!---Hidden Error message only shown when there is a validation error for departing and arrival--->
<label id="errorLocationFrom"></label>
</form>
<Form action="#" class="group" name="form2" id="Form-2" method="post" hidden = "true">
<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>
<input type="checkbox" id="Standard" name="Type" value="Standard">
<label class="light" for="Standard">Standard</label><br>
<input type="checkbox" id="First-Class" name="Type" value="First-Class">
<label class="light" for="First-Class">First Class</label><br><br>
<p id="total-cost"></p>
<button type = "button" value="checkout" id="checkoutbtn" onclick="AdultNumber(); calculateFare(); " >CHECKOUT</button>
</Form>
我正在制作一个铁路票务系统,我需要的是,首先验证要做的表格,然后单击"提交"按钮后,它将显示另一个表格,两个表格都在同一页上。
您可以将表单1的提交更改为输入类型按钮。当单击按钮1时,您将像以前一样进行验证,如果通过,则显示表单2。
在表单2中,您可以使用onsubmit事件将所需字段从表单1复制到表单2;<form id="Form-2" onsubmit="CopyData()">
在复制功能你可以做:
$('#Form-2').append( '<input type="hidden" name="LocationFrom" value="' + $('#LocationFrom').val() + '">' );
$('#Form-2').append( '<input type="hidden" name="LocationTo" value="' + $('#LocationTo').val() + '">' );
更新-小提琴:https://jsfiddle.net/blocknotes/copyuwyd/
相关文章:
- HTML多页表单显示隐藏部分,保留表单数据
- 如何在提交空白表单时隐藏图像
- HTML 表单中隐藏字段的限制
- 如何使用javascript在提交表单之前隐藏和显示元素
- 如何在Javascript中向表单添加隐藏的ID列表
- 如何用HTML表单发送隐藏的PHP变量
- 使用加载方法发送表单后隐藏“提交”按钮
- 从表单中隐藏accordian的标题(链接)
- 如何在MVC 4中使用jquery验证表单的隐藏输入字段
- 如何设置我的表单成为隐藏时加载页面
- Jquery邮件注册表单尝试隐藏表单后提交
- 将一个表单的值复制到另一个表单的隐藏字段中
- 设置表单内隐藏输入的值
- 在重置表单上隐藏ng消息
- jQuery在asp.net web表单中隐藏的html服务器控件复选框
- Javascript转换为java并发出表单和隐藏元素
- HTML表单元素隐藏的onload javascript
- 在HTML表单中隐藏多个字段
- 如何在使用Ajax/Jquery成功提交表单后隐藏表单
- 提交表单时隐藏DIV