最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting jQuery to make a new line inside a DIV when returning from an ajax request - Stack Overflow

programmeradmin4浏览0评论
 <script>   
     $.ajax
         ({
             url: "../getStatus/",
             dataType: 'json',
             success: function(json) 
             {            
                $('#ajaxLog').html(json.status+"<br/>");
             }
         });

</script>

 //Need this section to make a new line when refreshed
<div id = ajaxLog style="width:50%"></div>

This needs to be an accumulative, returned message to the webpage. Each time the process starts/stop/suspends it will be displayed. The displayed message will not keep saying stopped/started/ etc. when remaining in a state - rather displayed each time these states change then it wont be redisplayed constantly -- NOT like jquery.append() does.

Desired

started 10:48
stoped 10:50
Started 12:53
suspended 3:34
Started 3:40

NOT DESIRED

started 10:48
started 10:48
started 10:48
started 10:48
started 10:48
started 10:48
started 10:48
stoped 10:50
.
.

and so on

(like append() would do)

 <script>   
     $.ajax
         ({
             url: "../getStatus/",
             dataType: 'json',
             success: function(json) 
             {            
                $('#ajaxLog').html(json.status+"<br/>");
             }
         });

</script>

 //Need this section to make a new line when refreshed
<div id = ajaxLog style="width:50%"></div>

This needs to be an accumulative, returned message to the webpage. Each time the process starts/stop/suspends it will be displayed. The displayed message will not keep saying stopped/started/ etc. when remaining in a state - rather displayed each time these states change then it wont be redisplayed constantly -- NOT like jquery.append() does.

Desired

started 10:48
stoped 10:50
Started 12:53
suspended 3:34
Started 3:40

NOT DESIRED

started 10:48
started 10:48
started 10:48
started 10:48
started 10:48
started 10:48
started 10:48
stoped 10:50
.
.

and so on

(like append() would do)

Share Improve this question edited May 5, 2015 at 23:22 NissimL 1488 bronze badges asked Jun 27, 2011 at 15:14 stackoverflowstackoverflow 19.5k52 gold badges136 silver badges200 bronze badges 2
  • 1 I notice you've added </br> to your HTML. If this is what you are reporting not working then replace it with <br /> – Mutation Person Commented Jun 27, 2011 at 15:19
  • And what do you mean by 'accumulative return message'? Sorry, this doesn't make sense. Can you include some exampels of what you expect to soo. – Mutation Person Commented Jun 27, 2011 at 15:21
Add a ment  | 

5 Answers 5

Reset to default 9
    <script>   
    //Use this
    last=null;
     $.ajax
                ({
                    url: "../getStatus/",
                    dataType: 'json',
                    success: function(json) 
                {
                        //Replace this line
                        //$('#ajaxLog').html(json.status+"<br/>");

                        //with this:
                        if(last!=json.status)
                        {
                            $('#ajaxLog').append(json.status+"<br/>");
                            //or: $('#ajaxLog').append('<p>'+json.status+'</p>');
                            last=json.status;
                        }
                 }
             });

</script>

 //Need this section to make a new line when refreshed
<div id = ajaxLog style="width:50%"></div>

There is no such thing as new line in HTML. You could use a <br/> tag to achieve a new line semantics:

$('#ajaxLog').append(json.status + '<br/>');

Or put the contents into a div:

$('#ajaxLog').append('<div>' + json.status + '</div>');

Also you should use the .append() method instead of .html() to avoid overwriting the contents of the ajaxLog div each time this is invoked.

Add a line break in the HTML?

success: function(json) 
{
    $('#ajaxLog').html("<br />" + json.status+);
}

Unless I'm misreading this, you are calling the html() method which will replace the full contents of the div. Are you really trying to append the results (on a new line) to whatever is already inside the div?

My apologies if I'm not understanding the question correctly.

Shouldn't the ajax settings have an additional

data: ''

so it looks like

url: '../getStatus/', data: '', dataType: 'json', success: ...

? I'm pretty much sure I've read this somewhere.

发布评论

评论列表(0)

  1. 暂无评论