我使用AJAX和PHP獲取包含教育數據的對象數組。最多可以顯示4個不同的<select></select>
標籤。我怎樣才能爲每個選擇設置正確的選項並刪除重複?如何爲相同名稱的選擇設置選定的選項?
我想這一點,這會導致重複:
<div class="col-md-3">
<label>Levels</label>
<div class="form-group form-group-custom">
<select type="text" class="form-control" placeholder="Degree..." id="userEducationDegree educationCount + '" name="userEducationDegree[]">
<option value="' +data.degree_name + '">' + data.degree_name + '</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
然後我試圖刪除與JS,這僅與第一選擇標記工作重複:
var usedDegrees = {};
$("select[name='userEducationDegree[]'] > option").each(function() {
console.log("removing");
if(usedDegrees[this.text]) {
$(this).remove();
} else {
usedDegrees[this.text] = this.value;
}
});
電流輸出我有什麼用上面的設置是:
<option value="A">A</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
我想要的是<option value="A">A</option>
被刪除或<option>
被選中。
你如何識別重複?樣本輸入在哪裏?什麼是預期的輸出?實際產出是多少? – RobG
@RobG檢查編輯請求 – raqulka