我有一個頁面,當打開此頁面時,我想要轉到數據庫,檢索一些數據並將這些數據放在兩個選擇標記中。 我創建了它,一切正常,但我無法將返回的數據追加到選擇標籤。jQuery選擇標記的設置值
HTML代碼
<li>
<label>Related Concepts</label>
<p>
<label>Source</label>
<select name="source[]">
<option>select source</option>
</select>
</p>
<p>
<label>Destination</label>
<select name="destination[]">
<option>select destination</option>
</select>
</p>
</li>
jQuery代碼
$('#addRelation').ready(function(){
$.getJSON("http://localhost/Mar7ba/Ontology/getAllConcepts/TRUE",function(data){
var source = document.getElementsByName('source');
var des = document.getElementsByName('destination');
for(var i=0;i<data.length;i++){
source.innerHTML+="<option>"+data[i]+"</option>";
des.innerHTML+="<option>"+data[i]+"</option>";
}
});
});
我可以'不使用ID爲選擇標籤,因爲我會在我的頁面有很多選擇標籤,它們是自動創建的,所以我讓名稱作爲源[],所以我可以得到它的值後提交 –
我會嘗試你的代碼,謝謝 –
唯一讓我誤解你的選擇標記的是我不確定當你的名字值有尾括號時官方行爲(和選擇要求)是什麼:source [] – Kristian