多個選擇框
<td name="tutor_td"><select name="instructor_id" id="instructor_id">
<option th:value="0" >--請選擇--</option>
<option th:value="1" selected="selected" th:each="tutor : ${mainInstructor}"
th:text="${tutor == null?'':tutor.Account.name}"></option>
</select></td>
var tutor_select = $("td[name='tutor_td'] > select option:selected");
獲取所有td標籤下的select中被選中項,獲得結果如下
js部分
window.onload = function () {
TutorData = [
{"tutorName": "Chen", "tutorCode": "8150357188"},
{"tutorName": "Fujian", "tutorCode": "1902002742"},
{"tutorName": "童清", "tutorCode": "1810000317"}
];
//獲取所有td下select中option爲選中的jq對象
var tutor_selected = $("td[name='tutor_td'] > select option:selected");
$.each(TutorData,function (i,n) {
var tutorName = n.tutorName;
var tutorCode = n.tutorCode;
// 追加option
$("td[name='tutor_td'] > select").append(
"<option value="+tutorCode+">"+tutorName+"</option>"
)
$.each(tutor_selected,function (i,n) {
var tutor_name = $.trim(n.text);
var reg = /[\u4e00-\u9fa5]/g;
var name_ch = tutor_name.match(reg).join(""); //截取漢字
if (tutor_name == $.trim(name_ch)) {
n.value = tutorCode; //修改option的value
}
})
})
$.each(tutor_selected,function (i,n) {
if (n.value == '1') {
//$(n).attr("selected",false) ; //jq對象修改option的選中狀態
//n[i].selected = false; //dom對象修改option的選中狀態
n.text = '--請選擇--';
}
})
}