在发出ajax请求后触发js / jquery

时间:2013-11-15 06:05:29

标签: javascript ajax jquery

嗨我在加载htmlpage后正在进行ajax调用

<body onload="init_myfunc();">
<div class="left searchtop" id="suggestionbox" align="center">
-----------------------

我的init_myfunc()是......

function init_myfunc(lenletters)
{
  if (typeof lenletters =='undefined' ) {
    var search = '{{q_url}}';
    var query = search.split('?q=')[1];
    query = query.split('&')[0];
    var loc = search.split('&amp')[1];
    loc = loc.split('=')[1];
    var search_val = $("#autosuggest").val();
    requestData = {};
    if(typeof(Storage)!=="undefined"){
      requestData['rec_searches'] = localStorage.getItem('saved_searches');
    }
    requestData['page'] = 'searchboxlong'
    requestData['spellcheck'] = query;
    if(loc!= 'all')
      requestData['location'] = loc;

    $.get('/college-search/autosuggest/', requestData, function(data){$('#suggestionbox').html(data);});

   //$("#autosuggest").val(query); -- This is not working

  }
}

现在我想在ajax调用完成后调用这段代码

$("#autosuggest").val(query);

我尝试在我的ajax调用下面编写它,但它不起作用。一旦ajax加载......文本框变空

在ajax调用完成后,他们是否可以设置文本框的值

由于

1 个答案:

答案 0 :(得分:1)

你必须在成功回调中这样做。

 $.get(
     '/college-search/autosuggest/',
     requestData,
     function(data){
         $('#suggestionbox').html(data); 
         $("#autosuggest").val(query);
     }
 );