2016-02-18 31 views
0

下面是示例jQuery代碼如何在此jQuery代碼中添加變量?

$('#HomeCity option:selected').text() 

如何改變HomeCity到optionElement [關鍵]從功能的foreach?

我做

$(" '#' + optionElement[key] option:selected ").text() 

,但不工作。

回答

1

您應該正確引用以創建正確的選擇器。

使用

$("#" + optionElement[key] + " option:selected ").text() 
1

使其

$('#' + optionElement[key] + " option:selected ").text(); 

基本上optionElement[key]後,您需要重新啓動報價"和不需要雙引號'#'之前。

1

目前你提到的代碼不會工作,因爲你已經把它包裝在雙引號內。所以,按你的代碼

$(" '#' + optionElement[key] option:selected ").text() 

「#」 + optionElement [關鍵]選項:選擇是一個字符串,jQuery是搜索和它不你的頁面存在。

什麼,你應該做的,使其動態是

$("#"+optionElement[key]+"option:selected").text() 

這樣你optionElement [關鍵]是動態的,這將改變按照所選擇的選項。

希望這有些幫助。

快樂學習

相關問題