简体   繁体   English

提交表单后显示隐藏的DIV

[英]Display a hidden DIV after a form submit action

I am trying to show a hidden div after submitting a form data 提交表单数据后,我试图显示隐藏的div

Below is my form html where the input section will have a form to enter the data and after submitting the form I have to show hidden output section and show result there 下面是我的表单html,其中输入部分将具有输入数据的表单,提交表单后,我必须显示隐藏的输出部分并在其中显示结果

html code: html代码:

<div id="input">


----- some form datas here ----

<div id="submit">
        <input type="submit"  id="generate" name="script" value="generate" />

    </div>

</div>


<div id="output" style="display: none;">


--- php echo from above form------

</div>


</form>

css: CSS:

#input{
width:800px;
margin: auto;
border-width:1px;
border-style:solid;
border-color:#ddd;

}


#output{
width:800px;
margin: auto;
border-width:1px;
border-style:solid;
border-color:#ddd;

}

After going through some previous discussion about the same topics, below is one of the answered solution for this 在经历了有关相同主题的一些先前讨论之后,下面是针对此问题的已解决方案之一

create a JavaScript like to show the hidden div after form submission. 创建一个JavaScript,以便在提交表单后显示隐藏的div。

$('form').submit(function(){
    $('#output').show();  

});

or 要么

$('form').submit(function(e){
    $('#output').hide();  
    e.preventDefault();
    // Or with: return false;        
});

But both the solutions are not working for me.the second is able to show the hidden div output but it not showing actual form data. 但是这两种解决方案都不适合我。第二种方法可以显示隐藏的div output但不显示实际的表单数据。

How can I show it correctly ? 如何正确显示? I need to show this after form submission (type="submit") 我需要在表单提交后显示此信息(type="submit")

UPDATE: 更新:

  1. Removed the inline css to hide div 删除内联CSS以隐藏div

  2. Added css function in style sheet to hide div 在样式表中添加了CSS函数以隐藏div

     #output{ display:none; width:800px; margin: auto; border-width:1px; border-style:solid; border-color:#ddd; } 
  3. Added below jquery to show div on form submit 在jquery下面添加了以在表单提交上显示div

     $('form').submit(function(){ $('#output').css({ 'display' : 'block' }); }); 

Still I an not able to achieve the result. 我还是无法达到目的。 Any clue here ? 这里有什么线索吗?

use 采用

<form id="form" action="">
</form>

To display output 显示输出

 $(document).ready(function() {
        $('#output').hide();
        $('#form').submit(function(){
                 $('#output').show();   
        });
    });
$('form').submit(function(e){
    $('#doutput').hide();  
    e.preventDefault();
    // Or with: return false;        
});

Here in your script you are spelling doutput. 在脚本中,您正在拼写doutput。 replace it with output 用输出替换它

and to show use .css() function and define display: block !important; 并使用.css()函数display: block !important;并定义display: block !important; because you have displayed it in your inline style so to override this you need to set !important. 因为您已经以内联样式显示了它,所以要覆盖它,您需要设置!important。

Alternatively, define display: none; 或者,定义display: none; in the stylesheet instead of using in inline style and do the rest without setting !important 在样式表中而不是内联样式中使用其余样式,而无需设置!important

Remove Display: none; 删除显示:无;

And do this code 并执行此代码

$('form').submit(function(e){
    $('#doutput').hide();  
    e.preventDefault();
    // Or with: return false;        
});

to hide and 隐藏和

$('form').submit(function(e){
    $('#doutput').hide();  
    e.preventDefault();
    // Or with: return false;        
});

to show 显示

You could use .hide() and .show() as opposed to editing CSS attributes via .css() . 您可以使用.show() .hide().show() ,而不是通过.css()编辑CSS属性。

$('document').ready(function() {
    $('#output').hide();
    $('form').submit(function(e) {
        $('#output').show();
        e.preventDefault();
    });
});

if you want to show results within the DIV, there are many ways to do this. 如果要在DIV中显示结果,可以使用多种方法。

  1. Javascript Give all your form data an ID, and then write a javascript function to fill the answers in the said DIV. 为您的所有表单数据提供一个ID,然后编写一个JavaScript函数以在所述DIV中填写答案。
  2. Ajax Post your form to ajax, return the response to your DIV Ajax将您的表单发布到ajax,将响应返回到您的DIV
  3. New page request After submitting, check to see if $_POST is set and then show the div with $_POST contents. 新页面请求提交后,检查是否设置了$ _POST,然后显示具有$ _POST内容的div。

You could try using the "action" attribute of the form element or you could try the jquery serializeArray method at the form element right after the submit. 您可以尝试使用form元素的“ a​​ction”属性,也可以在提交后立即在form元素上尝试jquery serializeArray方法。

<form>
    <input type="text" name="giveNameHere" />
    <input type="submit"  id="generate" name="script" value="generate" />
</form>
<div id="output" style="display: none;">
    Output Content
</div>

$('form').submit(function () {
    $('#output').show();
    console.log($(this).serializeArray());
    return false;
});

Please see this jsfiddle 请看这个jsfiddle

If I got it right you don't want to reload the page. 如果我做对了,则您不想重新加载页面。 In this case you need to send the form data via ajax call. 在这种情况下,您需要通过ajax调用发送表单数据。 Than you can display the response and the hidden div. 比您可以显示响应和隐藏的div。

CSS 的CSS

#output {
    display:none;
}

jQuery jQuery的

$(document).ready(function(){

    $('form').submit(function(e){

        // Prevent the default submit
        e.preventDefault();

        // Collect the data from the form
        var formData =  $('form').serialize();

        //Send the from
        $.ajax({
            type: "POST",
            url: "generator.php",
            data: formData,

            success: function(msg){
                // Insert the response into the div
                $('#ajaxResponse').html(msg);

                // Show the hidden output div
                $('#output').slideDown();
            }
        });    
    });

});

HTML 的HTML

<div id="output">

    --- php echo from above form------

    <div id="ajaxResponse"></div>

</div>

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

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