我有像这样的jsp代码 我需要根据标签值和&需要设置选项作为验证,选项由数据库填写
<c:forEach var="roomOccupanice" items="${RoomOccRequestNow}">
<label value ="${roomOccupanice}" class="roomOccupanice" id="roomOccupanice">${roomOccupanice}</label>
<div class="custom-select-container">
<select id="roomType" name="roomType" class="roomType">
<option value="N/A" selected="selected">--Select--</option>
<c:forEach var="roomType" items="${RoomTypeRequestNow}">
<option value="${roomType}">${roomType}</option>
</c:forEach>
</select>
<label class="select-arrow"></label>
</div>
<!-- End -->
<div class="custom-select-container">
<select id="roomCount" name="roomCount" class="roomCount">
<option value="N/A" selected="selected">--Select--</option>
<c:forEach var="roomCount" items="${RoomCountRequestNow}">
<option value="${roomCount}">${roomCount}</option>
</c:forEach>
</select>
<label class="select-arrow"></label>
</div>
</c:forEach>
我的.js代码 这里是我在js中的验证部分
$(".roomOccupanice").each(function(){
var label = $(this).text(); // It will get current label text
$(".roomType").each(function(){
if (label == "Double"){ // i just hard cord "Double" now but it get from request
$(this).value = "Deluxe"; // need to set a value in select option it's already in one of options
}
});
});
需要将红色循环选择选项设置为&#34; Deluxe&#34;
答案 0 :(得分:1)
检查jQuery的val function:
$("#roomType").val("yourOptionName");
所以对你的例子来说:
$(".roomOccupanice").each(function () {
var label = $(this).text();
$(this).next(".custom-select-container").children("select").each(function() {
if (label == "Double") {
$(this).val("double");
}
});
});
答案 1 :(得分:1)
您需要过滤设置的选项以匹配属于当前标签的$('.roomtype', <ASSOCIATED SELECT ELEMENT>))
..
$(".roomOccupanice").each(function(){
var $t = $(this),
$select = $t.next().find('select');
if ($t.text().toLowerCase() == "double"){
$select[0].selectedIndex = $select.find('option').index($select.find('option[value^=Deluxe]')[0]);
}
});
答案 2 :(得分:0)
一种方法是通过索引
选择列表项document.getElementById("roomCount").selectedIndex = <yournumber>;