2012-11-02 52 views
0

我是相當新的jQuery和會很感激一些幫助如下:插入來自輸入API URL的jQuery

我有一個輸入表單,允許用戶選擇3個不同的地理區域(國家,地區和城市)所有信息都來自API。我需要來自國家的輸入(如用戶類型)進入URL調用「城市」的API。這樣用戶只能獲得與該國相關的城市。

這是我有:

<input type="text" name="country" id="country" /> 
<input type="text" name="city" id="city" /> 

$(document).ready(function() { 
     $("#country").tokenInput("http://someurl.com/search?q=", { 
      method: "POST", 
      searchDelay: 0, 
     }); 

     $("#city").tokenInput("http://someurl.com/search?q=[Insert values form country input field]", { 
      method: "POST", 
      searchDelay: 0, 
     }); 
    }); 

的jQuery插件,我使用的是:this

+0

你使用插件,需要研究插件的API文檔以瞭解如何添加數據。發佈一個鏈接插件文檔 – charlietfl

+0

嗨@charlietfl謝謝你。這裏是插件的鏈接:http://loopj.com/jquery-tokeninput/ –

回答

1

我沒有看到它的記錄,但插件代碼的來源允許一個函數用於url選項。

這應該工作:

$("#city").tokenInput(function(){ 
     return 'http://someurl.com/search?country='+ $('#country').val() +'q='; 
    }, { 
     method: "POST", 
     searchDelay: 0, 
}); 

如果它給你的任何問題,請檢查在瀏覽器控制檯的要求看生成的最終網址是什麼,或者看看是否有任何錯誤拋出

+0

Works。非常感謝 –

+0

你可能會使用功能/錯誤跟蹤器,並提到這可以記錄下來......我可能錯過了它的文檔,但我不認爲它在那裏 – charlietfl