简体   繁体   English

在AJAX发布后追加Div

[英]Appending Div after AJAX post

I'm struggling to understand why the following is occurring. 我正在努力理解为什么发生以下情况。 I am calling a very simple webservice as shown below. 我正在调用一个非常简单的Web服务,如下所示。 For some reason, my jquery appends the div, but it vanishes immediately? 由于某种原因,我的jquery追加了div,但是它立即消失了吗? Apologies for the poorly formatted thread... 不好意思的线程道歉...

Also, using jquery 1.7.1 as provided by VS2013 另外,使用VS2013提供的jquery 1.7.1

<WebMethod()>
Public Function GetTime() As String
    Dim time As String = ""
    time = Now().TimeOfDay.ToString

    Return time
End Function

My AJAX below 我的AJAX在下面

 function CallWebServiceFromJquery() {

         $.ajax({
             type: "POST",
             url: "WebService.asmx/GetTime",
             data: "{}",
             contentType: "application/json; charset=utf-8",
             dataType: "json",
             success: setTime,
             error: OnError

         });

     }

Success function 成功功能

function setTime(data, status) {
         //alert(data.d);
         $("#time").css("visibility", "visible")
         $("#time").append("<h1>" + data.d + "</h1>")
     }

Lastly, the onclick 最后,onclick

<asp:Button ID="btnCallWebService" runat="server" OnClientClick="CallWebServiceFromJquery()" Text="Call Webservice" />

The button click itself is causing a postback, hence why the appended div seems to disappear. 单击按钮本身会引起回发,因此为什么附加的div似乎消失了。 You need to stop the default event behaviour using preventDefault() : 您需要使用preventDefault()停止默认事件行为:

<asp:Button OnClientClick="CallWebServiceFromJquery(event)" ...
function CallWebServiceFromJquery(e) {
    e.preventDefault();
    $.ajax({
        // ajax settings...
    });
}

Make sure CallWebServiceFromJquery returns false . 确保CallWebServiceFromJquery返回false Based on your description of the div appearing and disappearing, your page is doing a postback. 根据您对div出现和消失的描述,您的页面正在回发。

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

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