首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果第一个窗体未正确验证,如何隐藏窗体?

如果第一个窗体未正确验证,如何隐藏窗体?
EN

Stack Overflow用户
提问于 2015-12-01 17:53:22
回答 1查看 33关注 0票数 1

JavaScript:

代码语言: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:

代码语言:javascript
复制
<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>

我正在制作一个铁路票务系统,我需要做的是,该表单首先被验证,然后点击提交按钮,它将显示另一个表单,两个表单都在同一个页面上。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-01 21:35:52

您可以将form1的提交更改为输入类型按钮。单击button1时,您可以像以前一样进行验证,如果通过验证,则显示form2。

在form2中,可以使用onsubmit事件将需要的字段从form1复制到form2;<form id="Form-2" onsubmit="CopyData()">

在复制函数中,您可以:

代码语言:javascript
复制
$('#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/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34026879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档