2017-10-16 107 views
0

我試圖根據點擊事件更改AJAX URL參數。例如,我有一個數據庫,如:嘗試根據點擊事件更改AJAX url參數

http://test/test/test/state/NJ

這可以改變,以反映對於特定狀態的數據。因此,如果您在網址末尾輸入「FL」而不是「NJ」,則會顯示佛羅里達州而非NJ的數據。我的問題是我正在使用美國的地圖,當你點擊某個狀態時,我需要點擊事件來動態地改變AJAX網址。請參閱下面的代碼謝謝。我不知道爲什麼點擊事件不會改變數據的U​​RL參數。

$.ajax({ 
     dataType: "jsonp", 
     url: $(this).data('url'), 
     cache: true, 
     success: function(json){  

     } 
}); 

$('#mapWhereToBuy').usmap({ 
    click: function(event, data) { 
    var description = "Sorry. We do not carry products in this state."; 
    switch(data.name) 
    { 

     case 'NJ': 
      description = 'New Jersey'; 
      $(this).attr('data-url', 'http://test/test/test/state/NJ'); 
      break; 
     case 'PA': 
      description = 'Pennsylvania'; 
      $(this).attr('data-url', 'http://test/test/test/state/PA'); 
      break; 
    } 

    $('#clicked-state').text(description); 

    } 
}); 

console.log($(this).data('url')); 

回答

1

我覺得你讓它變得很困難。你可以在.usmap內發射ajax。看下面的例子。

$('#map').usmap({ 
    click: function(event, data) { 
    switch(data.name){ 
     case 'NJ': 
      description = 'New Jersey'; 
      break; 
     case 'PA': 
      description = 'Pennsylvania'; 
      break; 
     default: 
      description = 'Sorry. We do not carry products in this state.'; 
      break; 

    } 
    $.ajax({ 
      dataType: "jsonp", 
      url: 'http://test/test/test/state/'+data.name, 
      cache: true, 
      success: function(json){ 
      //Organized ajax returned data from here. 
      } 
     }); 
    } 
}); 
+1

非常感謝!這正是我需要的 – Tom