我使用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