2016-04-15 25 views
-1

私はオートコンプリート機能を備えた検索フォームを作成中です。私のオートコンプリートは正常に動作しています。私はオートコンプリート値が設定された後、別のPHPページにデータを送信したい。下の は私のjquery for autocompleateです。jqueryでajaxが成功した後にPHPページにリダイレクト

function autocomplet() { 
    var min_length = 1; // min caracters to display the autocomplete 
    var keyword = $('#searchitem').val(); 
    var search_location = $('#search_location').val(); 
    var datastring= 'keyword='+ keyword + '&search_location='+search_location; 
    if (keyword.length >= min_length) { 
     $.ajax({ 
      url: 'global_search.php', 
      type: 'POST', 
      data: datastring, 
      success:function(data){ 
       $('#search_list_id').show(); 
       $('#search_list_id').html(data); 
      } 
     }); 
    } else { 
     $('#search_list_id').hide(); 
    } 
} 
// set_item : this function will be executed when we select an item 
function set_item(item) { 
    // change input value 
    $('#searchitem').val(item); 

     window.location.href = 'searchtestr.php?key=' + data 
    // hide proposition list 
    $('#search_list_id').hide(); 
} 

私はwindow.location.href = 'searchtestr.php?key=' + dataにページをリダイレクトしようとしましたが、動作しませんでした。条件は、オートコンプリートから何かが選択された後にリダイレクトされるべきページです。あなたのsuccessハンドラでごset_itemメソッドを呼び出す必要があり

答えて

0

:また

success:function(data){ 
     $('#search_list_id').show(); 
     $('#search_list_id').html(data); 
     set_item(data.item); //data.something 
    } 

を、あなたは、次のページにdataという変数を渡すようにしようとしているが、set_item方法は、その変数へのアクセスを持っていません。 itemなどを送信する必要があります。 window.location.href = 'searchtestr.php?key=' + item;

また、SPAで何かしているのかどうかはわかりません(推測しません)...ページをリダイレクトした後にDOMを変更することは意味がないと思います。 $('#search_list_id').hide();および$('#searchitem').val(item);window.location.hrefがAJAX呼び出しを開始しておらず、ページ全体がリダイレクトされることにご注意ください。

0

は、私は以下のようにしようと、それが働いていたこの

function autocomplet() { 
 
    var min_length = 1; // min caracters to display the autocomplete 
 
    var keyword = $('#searchitem').val(); 
 
    var search_location = $('#search_location').val(); 
 
    var datastring= 'keyword='+ keyword + '&search_location='+search_location; 
 
    if (keyword.length >= min_length) { 
 
     $.ajax({ 
 
      url: 'global_search.php', 
 
      type: 'POST', 
 
      data: datastring, 
 
      success:function(data){ 
 
       $('#search_list_id').show(); 
 
       $('#search_list_id').html(data); 
 
      }, 
 
\t \t \t change: function (event, ui) { 
 
\t \t \t if (ui.item == null || ui.item == undefined) { 
 
\t \t 
 
\t \t \t } else { 
 
\t \t \t \t window.location.href = 'searchtestr.php?key=' + data; 
 
\t \t \t } 
 
     }); 
 
    } else { 
 
     $('#search_list_id').hide(); 
 
    } 
 
}

+0

エラーが発生していますReferenceError:set_itemが定義されていません –

-1

を試してみてください。私の懸念は、価値がPHPページにリダイレクトされた後です。

function autocomplet() { 
    var min_length = 1; // min caracters to display the autocomplete 
    var keyword = $('#searchitem').val(); 
    var search_location = $('#search_location').val(); 
    var datastring= 'keyword='+ keyword + '&search_location='+search_location; 
    if (keyword.length >= min_length) { 
     $.ajax({ 
      url: 'global_search.php', 
      type: 'POST', 
      data: datastring, 
      success:function(data){ 
       $('#search_list_id').show(); 
       $('#search_list_id').html(data); 
      } 
     }); 
    } else { 
     $('#search_list_id').hide(); 
    } 
} 
// set_item : this function will be executed when we select an item 
function set_item(item) { 
    // change input value 
    $('#searchitem').val(item); 
    // hide proposition list 
    $('#search_list_id').hide(); 

    var location = $('#search_location').val(); 
    var search_term = $('#searchitem').val(); 

    if(search_term != ''){ 
    window.location.href = 'searchtestr.php?location=' + location + '&search_term='+ search_term; 
    } 
} 

ありがとうございました。

+0

なぜダウンリストに投票したのか分かりますか? –

関連する問題