简体   繁体   English

如何在加载时显示隐藏的表单

[英]how to show hidden form on load

I have a JSP page in which there are two forms. 我有一个JSP页面,其中有两种形式。

  • One is the default and another is hidden. 一个是默认设置,另一个是隐藏的。
  • On default form, there is a link to the hidden form. 在默认表单上,有指向隐藏表单的链接。 On click of this link, the hidden form appears which has some input fields and submit button. 单击此链接后,将显示隐藏的表单,其中包含一些输入字段和提交按钮。
  • When I click on submit, obviously the form gets submitted. 当我单击提交时,显然表单已提交。

But when the output comes, the page gets loaded and shows the default form. 但是,当输出到来时,页面将被加载并显示默认表单。 I want to show hidden form instead. 我想显示隐藏形式。 What should I do? 我该怎么办?

My JSP page has following code structure: 我的JSP页面具有以下代码结构:

<div id="1st_form">

    <form id="defaultForm"  action="/searchModel/search">
       %{-- form input fields--}%

            <div style="float: left; margin-left: 15px;">
                <a href="javascript:advancedSearch();" style="color: #ffffff; font-size: 13px;">Advanced Search</a>
            </div>


    </form>
</div>

<div id="2nd_form" hidden="hidden">

    <form id="hiddenForm"  action="/searchModel/search">
       %{-- form input fields--}%
            <input type="submit" id="searchButton" value="Advanced Search" >

            <div style="float: left; margin-left: 15px;">
                <a href="javascript:normalSearch();" style="color: #ffffff; font-size: 13px;">Advanced Search</a>
            </div>


    </form>
</div>

And javascript functions are as follows: 和javascript函数如下:

function advancedSearch(){
        $("#1st_form").hide();
        $("#2nd_form").show();

    }

    function normalSearch(){
        $("#2nd_form").hide();
        $("#1st_form").show();

    }

The problem is, when you send any HTML page to the client, all changes you did with Javascript previously will be lost. 问题是,当您将任何HTML页面发送到客户端时,您之前使用Javascript所做的所有更改都会丢失。 That is why you see the default form visible again after submit. 这就是为什么您在提交后再次看到默认表单的原因。

What you want to do is: 您想要做的是:

When the "Advanced Search"-button is clicked, show the "Advances Search" form and make the "Default" form hidden in the response . 单击“高级搜索”按钮时,显示“高级搜索”表单,并将“默认”表单隐藏在响应中

What is something you need to do on server-side, because the client does not know which button submitted the form to the server. 您需要在服务器端执行什么操作,因为客户端不知道哪个按钮向服务器提交了表单。

So, you need check if the form was submitted by the "Advanced Search"-button on server side. 因此,您需要检查表单是否由服务器端的“高级搜索”按钮提交。 To be able to do this, you need to give the button a name: 为此,您需要给按钮命名:

<input type="submit" id="searchButton" 
       name="searchButton" value="Advanced Search" >

Now, when this button is clicked, it's value will be sent to the server . 现在,单击此按钮后, 其值将发送到服务器 In this case, Advanced Search . 在这种情况下,请使用Advanced Search

Next, you need to check if this value was sent to the server in your JSP: 接下来,您需要检查此值是否已发送到JSP中的服务器:

<div id="1st_form" ${param.searchButton == 'Advanced Search'?'hidden':''} >
    ...
</div>
<div id="2nd_form" ${param.searchButton == 'Advanced Search'?'':'hidden'} >
    ...
</div>

Note: param is an implicit object in Expression Language . 注意: paramExpression Language中隐式对象

This way, when you first open the page, param.searchButton will be empty and it will show the default search. 这样,当您第一次打开页面时, param.searchButton将为空,并显示默认搜索。 When the "Advanced Search" button was submitted, param.searchButton will be Advanced Search , and the advanced search will be visible. 当“高级搜索”按钮提交, param.searchButtonAdvanced Search和高级搜索将是可见的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM