2012-01-30 26 views
0

所以這裏是我的問題jQuery的,點擊列表項Q

我與的getJSON獲得的數據和我犯了一個選項列表,這是地區名單,這是好的,現在我需要的功能,其中點擊列表項我得到與城鎮的名單另一個下拉菜單爲該區域

所以我的代碼有云:

var encodedurl=escape("http:path"); 
$.getJSON("proxy3.php?url="+ encodedurl, 

function(data) 
{ 
$.each(data, function(i, item){ 
$("#grad_search").append('<option>' +item.city + "</option>"); 
}); 

,所以我需要從JSON符合var encodedurl=escape("http:path");發送變量,因此,當我click列表項我送數據到另一個選擇選項

var encodeurl_area=escape("http://some_path" + VARIABLE + "more_path"); 
$.getJSON("proxy3.php?url="+ encodeurl_area, 
function(data) 
{ 
$.each(data, function(i, item){ 
          $("#kvart_search").append('<option>' + item.city_area + '</option>'); 
});  
}); 

這樣整個我的問題是如何在鏈接獲得變量上,我需要從我的第一個功能

+0

什麼是'VARIABLE'?所選選項的文字?如果是這樣,請看看http://stackoverflow.com/questions/196684/jquery-get-select-option-text – 2012-01-30 15:18:06

+0

如果可能,請顯示所有代碼,我假設這是一個單擊事件處理程序? – 2012-01-30 15:19:37

+0

ShankarSangoli給答案我一直在尋找,是的,你是對的,這是單擊事件處理 – hullfan 2012-02-01 12:57:16

回答

1

獲得的價值有幾件事情你應該注意你的代碼。

  1. 在開始追加新城市(選項)之前,您應該清除列表。
  2. 同樣適用於小鎮名單也
  3. 當您填充選項,您應該有每個選項的值。這個值可以用來傳遞下一個列表來填充。

更改您這樣的代碼。

var encodedurl=escape("http:path"); 
$.getJSON("proxy3.php?url="+ encodedurl, 

function(data) 
{ 
    //Look here I am using empty method to clear the existing list 
    $("#grad_search").empty() 
    $.each(data, function(i, item){ 
     $("#grad_search").append('<option value="' 
         + item.cityId + '">' +item.city + "</option>"); 
    }); 
} 

//Handle the change event and pass the selected value to get city areas(towns) 
$("#grad_search").change(function(){ 
    var encodeurl_area=escape("http://some_path" + $(this).val() + "more_path"); 
    $.getJSON("proxy3.php?url="+ encodeurl_area, function(data) 
    { 
     //Look here I am using empty method to clear the existing list 
     $("#kvart_search").empty() 
     $.each(data, function(i, item){ 
      $("#kvart_search").append('<option value="' 
         + item.cityAreaId + '">' + item.city_area + '</option>'); 
     });  
    }); 
}); 
+1

你怎麼總是知道OP的意圖是什麼?我讀了很多問題,他們提出的問題對我來說毫無意義。然後你的答案解釋他們的實際問題。也許ESP? – mrtsherman 2012-01-30 15:28:23

+0

可能是經驗幫助我。 – ShankarSangoli 2012-01-30 15:33:34

+0

是的。就是這樣,完全忘記了「option」標籤下的「value」選項 – hullfan 2012-01-31 08:07:01

0

得到變量,您可以通過

$(document).delegate("#grad_search","change",function(){ 

var variable = $(this).val(); //here you will get the selected value 
}); 
+0

@mrtsherman正要解決更多的數據,其固定 – Rafay 2012-01-30 15:23:09