简体   繁体   English

使用JQuery更新表单提交中显示的div

[英]Update displayed div on form submit using JQuery

I have a form as follows : 我的表格如下:

    <div class="myForm">
        <input name="fname" id="fname" value="First Name" onfocus="if (this.value == 'First Name') {this.value = '';}" onblur="if (this.value == '') {this.value = 'First Name';}" type="text"  />
        <input type="button" value="Submit" id="btnSubmit">
    </div>

Then, in my JQuery header I have a script defined as follows : 然后,在我的JQuery标头中,我定义了一个脚本,如下所示:

<script>
             $(document).ready(function(){ 
                 $("#btnSubmit").click(function(){
                     var fname = $("#fname").val();
                     $.ajax({
                         url : "updateDb.php", // php file where you write your mysql query to store value
                         type : "POST", // POSTmethod
                         data : {"fistname":fname},
                         success : function(n){
                                                 alert("works"); //function goes here
                         } 
                     });
                 });

What I want is so that when the user presses submit, the form disappears and a simple screen saying "your details have been entered" appears in its stead. 我想要的是这样,当用户按下“提交”时,表格消失了,取而代之的是一个简单的屏幕,上面写着“您的详细信息已输入”。 I'm thinking the best way of doing this is to have two divs, one as about and another simple 我在想这样做的最好方法是有两个div,一个大约和另一个简单

Your details have been entered 您的详细信息已输入

kind of jobby. 种工作。 Then when the function is called I could change one of the divs to collapsed and the other to visible. 然后,当调用函数时,我可以将div之一更改为折叠状态,将另一个更改为可见状态。

Is that the best way for me to do this? 那是我这样做的最好方法吗? What are my alternatives? 我有什么选择? How can I write the code to accomplish this ? 我该如何编写代码来完成此任务?

Thanks a lot. 非常感谢。

It depends on whether the form will be used again. 这取决于是否再次使用该表格。 If not, you can just empty it and provide the text: 如果没有,您可以将其清空并提供文本:

$('.myForm').empty().text('Your details have been entered.');

Something simple like that just empties the div and plugs in the text. 诸如此类的简单操作只是清空div并插入文本。 If you're curious why I didn't use .html() , its just personal preference (I think its more transparent this way), but it is also remarkably faster . 如果您好奇为什么我不使用.html() ,它只是个人喜好(我认为这样更透明),但是它也明显更快

If you want to reload the form to be used multiple times, I would create another div, set it to display:none; 如果要重新加载要使用多次的表单,我将创建另一个div,将其设置为display:none; , and toggle them: ,然后切换它们:

HTML: HTML:

<div class="myFormResponse">Your details have been entered.</div>

CSS: CSS:

.myFormResponse {
    display:none;
}

jQuery: jQuery的:

$('.myForm,.myFormResponse').toggle();

This also sets up calling that exact same function if you want to load the form again. 如果要再次加载表单,这还将设置调用完全相同的函数。

That should handle either scenario. 那应该处理两种情况。

替换表单的HTML应该可以。

$('#complete-message').html("<h1>Form Submitted!</h1><h2>We will be in touch soon.</h2><img id='checkmark' src='image' />");

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

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