<?php for($i = 1; $i <=5; $i++ ){?>
<tr id="row">
<td class="input-field col s2">
<label>Week Days</label>
<select id="week_days<?php echo $i;?>" data-rel="chosen" name="week_days<?php echo $i;?>[]" class="form-control" multiple="multiple">
<option value="1">Monday</option>
<option value="2">Tuesday</option>
<option value="3">Wednesday</option>
<option value="4">Thursday</option>
<option value="5">Friday</option>
</select>
</td>
</tr>
<?php } ?>
<a id="more_btn" class="right" href="javascript:void(0);"><img src="../../assets/images/icons/add-icon.ico" width="23px" title="Add More"/></a>
如何使用jquery获取option的值?
问题是我在点击锚标记上得到了一个空值。适当是获得期权价值的正确方法。第一个用户选择一个星期一选项,然后单击添加更多锚标记,然后在警报中显示正确的1个值,但是当用户更改一个选项并单击添加更多锚标记,然后警报将其显示为空。为什么显示空而不是1,2,3,4,5?
<script type="text/javascript">
var j=1;
$('#more_btn').click(function() {
j++;
var u = j-1;
var prev_weekdays = $('#week_days'+u).val();
alert(prev_weekdays);
});
</script>
答案 0 :(得分:0)
当more_btn链接单击时,获取选定的工作日值并发出警报,请参见示例 例如-
<script type="text/javascript">
$('#more_btn').click(function() {
var prev_weekdays = $('#week_days').find(":selected").text();
alert(prev_weekdays);
});
</script>
将您的选择设为
<select id="week_days" data-rel="chosen" class="form-control" multiple="multiple">
<option value="1">Monday</option>
<option value="2">Tuesday</option>
<option value="3">Wednesday</option>
<option value="4">Thursday</option>
<option value="5">Friday</option>
</select>
告诉我这是否有效
答案 1 :(得分:0)
希望这可以解决您的问题
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Get Values from Multiple Select Box</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("button").click(function(){
var countries = [];
$.each($(".country option:selected"), function(){
countries.push($(this).val());
});
alert("You have selected the country - " + countries.join(", "));
});
});
</script>
</head>
<body>
<p>Press control key (Ctrl) to select multiple values:</p>
<form>
<label>Country:</label>
<select class="country" multiple="multiple" size="5">
<option value="1">United States</option>
<option value="2">India</option>
<option value="3">United Kingdom</option>
<option value="4">Brazil</option>
<option value="5">Germany</option>
</select>
<button type="button">Get Values</button>
</form>
</body>
</html>