javascript Ajax 函数成功时显示消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18172905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:57:23  来源:igfitidea点击:

Show message when Ajax function succeeds

javascriptajaxjquery

提问by tatty27

I would like to display a message when an Ajaxfunction has succeeded but being new to AjaxI am struggling

我想在Ajax功能成功时显示一条消息,但Ajax我是新手我很挣扎

function save_pos_reasons()
  {
    $.ajax({
    type: "POST",
    url: "save_pos_reasons.php",
    data: $('#add_positioning').serialize(),
    success: function() {
       $("#successMessage").show();
        }
    });
 }


<div id="successMessage"> It worked </div>

At the moment this does nothing although the actual function is working

目前,尽管实际功能正在运行,但它什么也没做

采纳答案by MrCode

The message div needs to be hidden by default, otherwise .show()will not do anything if it's already visible.

消息div需要默认隐藏,否则.show()如果它已经可见就不会做任何事情。

CSS:

CSS:

#successMessage {
    display:none;
}

Or inline:

或内联:

<div id="successMessage" style="display:none;"> It worked </div>

回答by SarathSprakash

Try this simple code . Only if the ajax is working the message will be displayed

试试这个简单的代码。只有当 ajax 正在工作时,才会显示消息

$.ajax({
        type: "POST",
        url: "save_pos_reasons.php",
        data: $('#add_positioning').serialize(),
        success: function() {
           $("#successMessage").html("It worked");
            }
        });

the html

html

<div id="successMessage"> </div>

Hope this helps. Thank you

希望这可以帮助。谢谢