我有一个带有复选框的多选下拉列表(jquery includeSelectAllOption),我只想要最后选择的值而不是所有选中的复选框值。
<select class="form-control" id="change_city" multiple="multiple">
<?php foreach ($city_array as $key => $value) {
# code...
echo "<option value=".$value['city_id'].">".$value['city_name']."</option>";
} ?>
</select>
<script>
$(function() {
$('#change_city').multiselect({
includeSelectAllOption: true
});
});
</script>
答案 0 :(得分:0)
那么,只需将其作为额外选项并删除includeSelectAllOption
即可。当用户点击&#34;选择全部&#34;时,取消选择所有其他选项并保持&#34;全选&#34;。
var handler = function(){
let val = $(this).val();
if(val === null) return;
val.forEach(function(value){
if(value == "all"){
$("#change_city option:selected").prop("selected", false);
$('#change_city option[value="all"]').prop('selected', true);
}
});
}
$("#change_city").on("change", handler);
&#13;
#change_city {
height: 150px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="form-control" id="change_city" multiple="multiple">
<option value="1">City 1</option>";
<option value="2">City 2</option>";
<option value="3">City 3</option>";
<option value="4">City 4</option>";
<option value="all">Select All</option>";
</select>
&#13;
答案 1 :(得分:0)
见下面的代码我希望可以帮到你
<label>Country:</label>
<select class="country" multiple="multiple" size="5">
<option>United States</option>
<option>India</option>
<option>United Kingdom</option>
<option>Brazil</option>
<option>Germany</option>
</select>
<button type="button">Get Values</button>
<script type="text/javascript">
$(document).ready(function() {
$("button").click(function(){
var countries = [];
$.each($(".country option:selected"), function(){
countries.push($(this).val());
});
//countries.join(", ")
alert("You have selected the country - " + countries.slice(-1)[0]);
});
});
</script >