jquery的奇怪行为 - append()不起作用

时间:2022-08-10 21:36:46

this is my script:

这是我的脚本:

$(document).ready(function() {
  $.post("mysql_calendar/json_fail.php",
    function (data) {
      var sel = $( "#availableTime" );
      sel.empty();
      for (var i=0; i<20; i++)
        sel.append( 'val' + i + '; ' );
      sel.append ( 'test; ' ) ;
  }, "json");
  var ss = $( "#availableTime" );
  ss.empty();
  ss.append ( 'not working 1; ', 'not working 2; ', 'not working 3; ' ) ;

json_fail.php:

< ? php
echo '[]';
 ? >

Results of this script: Laikas:val0; val1; val2; val3; val4; val5; val6; val7; val8; val9; val10; val11; val12; val13; val14; val15; val16; val17; val18; val19; test;

这个脚本的结果:Laikas:val0; VAL1; VAL2; VAL3; VAL4; val5; VAL6; val7;的Val8; val9; val10; VAl11难; val12; val13; val14; val15; val16; val17; val18; val19;测试;

Why ss.empty () and ss.append is not working? If I delete $.post part, it starts working. Also, if I debug it step by step with firebug, everything works as expected.

为什么ss.empty()和ss.append不起作用?如果我删除$ .post部分,它就会开始工作。此外,如果我使用firebug逐步调试它,一切都按预期工作。

2 个解决方案

#1


1  

what's the expected behavior? Keep in mind that $.post is asynchronous, so first ss will be emptied, then you append 'not working1;...'.

什么是预期的行为?请记住$ .post是异步的,所以首先ss将被清空,然后你追加'not working1; ...'。

Then the $.post callback occurs, empties it again and appends the 'val' strings.

然后发生$ .post回调,再次清空它并附加'val'字符串。

#2


0  

The post is asynchronous which causes the succss function to be called after the post has been completed.

帖子是异步的,这导致在帖子完成后调用succss函数。

In effect you are doing this:

实际上你是这样做的:

  $.post("mysql_calendar/json_fail.php", //1) Post begins to process.
    function (data) { //5) once complete call this
      var sel = $( "#availableTime" ); //6) get ss
      sel.empty(); //7) call empty items added in 4) will be cleared.
      for (var i=0; i<20; i++) //8) append some text
        sel.append( 'val' + i + '; ' );
      sel.append ( 'test; ' ) ;
  }, "json");
  var ss = $( "#availableTime" ); //2) get ss
  ss.empty(); //3) call empty
  ss.append ( 'not working 1; ', 'not working 2; ', 'not working 3; ' ) ; //4) Append stuff

What do you intend as your end result?

你最终的结果是什么?

#1


1  

what's the expected behavior? Keep in mind that $.post is asynchronous, so first ss will be emptied, then you append 'not working1;...'.

什么是预期的行为?请记住$ .post是异步的,所以首先ss将被清空,然后你追加'not working1; ...'。

Then the $.post callback occurs, empties it again and appends the 'val' strings.

然后发生$ .post回调,再次清空它并附加'val'字符串。

#2


0  

The post is asynchronous which causes the succss function to be called after the post has been completed.

帖子是异步的,这导致在帖子完成后调用succss函数。

In effect you are doing this:

实际上你是这样做的:

  $.post("mysql_calendar/json_fail.php", //1) Post begins to process.
    function (data) { //5) once complete call this
      var sel = $( "#availableTime" ); //6) get ss
      sel.empty(); //7) call empty items added in 4) will be cleared.
      for (var i=0; i<20; i++) //8) append some text
        sel.append( 'val' + i + '; ' );
      sel.append ( 'test; ' ) ;
  }, "json");
  var ss = $( "#availableTime" ); //2) get ss
  ss.empty(); //3) call empty
  ss.append ( 'not working 1; ', 'not working 2; ', 'not working 3; ' ) ; //4) Append stuff

What do you intend as your end result?

你最终的结果是什么?