jQuery .html()的回调函数?

Toh*_*hid 23 javascript ajax jquery json

我有以下代码:

$.ajax({
      type: 'GET',
      url: 'index.php?route=checkout/onepagecheckout/getpaypaldata',
      dataType: 'json',
      success: function(json) {
                              $('#pp_info').html(json['output']);
                              $('#payment').submit();
                              }
      });
Run Code Online (Sandbox Code Playgroud)

ajax请求接收包含html表单的json对象,如:

<form id="payment" method="post" action="https://www.paypal.com/cgi-bin/webscr">
<input type="hidden" value="_cart" name="cmd">
<input type="hidden" value="1" name="upload">
<input type="hidden" value="test@yahoo.ca" name="business">
<input type="hidden" value="Sample Item Name" name="item_name_1">
<input type="hidden" value="TESTI-1" name="item_number_1">
<input type="hidden" value="104.98" name="amount_1">
<input type="hidden" value="1" name="quantity_1">
<input type="hidden" value="0" name="weight_1">
<input type="hidden" value="Type" name="on0_1">
<input type="hidden" value="As Shown" name="os0_1">
<input type="hidden" value="Delivery Date" name="on1_1">
<input type="hidden" value="Jun 23,2012" name="os1_1">
<input type="hidden" value="Comments" name="on3_1">
<input type="hidden" value="test message" name="os3_1">
</form>
Run Code Online (Sandbox Code Playgroud)

其中包含PayPal处理订单所需的信息.一切正常,但我相信有时在jQuery .html函数加载html内容之前提交表单.

是否有.html的回调函数?或者我可以用来解决问题的任何其他方法?PayPal数据以HTML形式出现,我无法更改该部分,所以我只有一个选项,它以某种方式加载html内容并提交表单!

The*_*pha 46

你可以试试这个

success: function(json) {
    $('#pp_info').html(json['output']).promise().done(function(){
        $('#payment').submit();
    });
}
Run Code Online (Sandbox Code Playgroud)

  • 这只是setTimeout(fn)所做的./sf/ask/3156015831/#45086288 html方法没有回调,这个承诺没有等待要完成的html方法.它只是将回调推送到回调队列,这使得它在运行附加的html中的javascript之后运行.在这种情况下,setTimeout会更加清晰,而不是让人们认为.html()可以有一个回调. (5认同)
  • jQuery [promise](http://api.jquery.com/promise/)和[detail](http://net.tutsplus.com/tutorials/javascript-ajax/wrangle-async-tasks-with-jquery-promises /). (4认同)