简体   繁体   English

提交表单后未显示“谢谢”消息

[英]Thank You message not displayed after Form submission

I was trying to display a thank you message in the same page after the submission of a form. 提交表单后,我试图在同一页面上显示感谢消息。 However, even though the form gets submitted correctly, the message does not gets displayed. 但是,即使正确提交了表单,也不会显示该消息。

I'm using the following javascript to cater to this: 我正在使用以下JavaScript来满足此要求:

$(function () 
        {
            $('form').submit(function (e) 
            {
                e.preventDefault();
                $.ajax(
                {
                    url: this.action,
                    type: this.method,
                    data: $(this).serialize(),
                    success: function (result) 
                    {
                        if(result.indexOf("success") > -1)
                        {
                            document.getElementById("thankyou_message").style.display = "inline";
                        }
                    }
                });
            });
        });

The response of the url is something like this: 网址的响应如下所示:

{"result":"success","data":...

I've my code in the following codepen URL: http://codepen.io/abbor123/pen/EgjLdR 我的代码位于以下代码笔URL中: http ://codepen.io/abbor123/pen/EgjLdR

Can you please throw some light into the issues with my coding that is hampering the display of the text in the page? 您能否说明一下我的编码中妨碍页面文本显示的问题?

PS: I'm a beginner and hence I request you to be gentle on me. PS:我是一个初学者,因此我要求您对我保持温柔。 :) :)

Thanks. 谢谢。 AB AB

I think that you should try this code if your sample data is right : 我认为,如果您的示例数据正确,则应该尝试以下代码:

if(result.result.indexOf("success") > -1){
 document.getElementById("thankyou_message").style.display = "inline";
}

You are doing indexOf in JSON object you should do this on the string 您正在JSON对象中执行indexOf,则应在字符串上执行此操作

http://www.w3schools.com/jsref/jsref_indexof.asp http://www.w3schools.com/jsref/jsref_indexof.asp

from the response you posted, I assume that you got a json response. 从您发布的响应中,我假设您收到了json响应。 then you can check it like this. 那么您可以像这样检查它。

success: function(response) {
    if(response.result == 'success') {
      document.getElementById("thankyou_message").style.display = "inline"; 
    } else {
      document.getElementById("thankyou_message").style.display = "none";
    }
}

check this sample code, which returns a json response. 检查此示例代码,该示例代码返回json响应。 https://gist.github.com/rashivkp/ef581ea4f4d4e7fba90086ea2c6bb5d2 https://gist.github.com/rashivkp/ef581ea4f4d4e7fba90086ea2c6bb5d2

try replace: 尝试替换:

document.getElementById("thankyou_message").style.display = "inline";

to: 至:

$('#thankyou_messa').fadeIn();

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

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