簡體   English   中英

如果第一個表單未正確驗證,如何隱藏表單

[英]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>

我正在制作一個鐵路票務系統,我需要做的是首先驗證表單,然后單擊“提交”按鈕后,它將顯示其他表單,兩個表單都在同一頁面上。

您可以將form1的提交更改為輸入類型按鈕。 單擊button1時,您將像以前一樣進行驗證,如果通過,則顯示form2。

在form2中,可以使用onsubmit事件將所需的字段從form1復制到form2。 <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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM