在 php 中,我们可以使用选择选项,因为用户可以选择多个选项值,之后我单击移动按钮,在按钮中写入一个单击函数,现在如何在单个 onclick 中获取所有选定的选项值。
<button type="button" id="search_rightSelected" onclick="inventory_left_to_right(document.getElementById('search'),false)" class="btn btn-block"><i class="glyphicon glyphicon-chevron-right"></i></button>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.0.min.js"></script>
<select id="search" multiple>
<option value="1">Volvo</option>
<option value="2">Saab</option>
<option value="3">Opel</option>
<option value="4">Audi</option>
</select>
<button type="button" id="search_rightSelected" onclick="inventory_left_to_right()" class="btn btn-block"><i class="glyphicon glyphicon-chevron-right"></i> HHH </button>
<script>
function inventory_left_to_right() {
//var allVal = '';
var allValArr = [];
$("#search :selected").each(function() {
// you can use to get the values $(this).val();
//allVal += $(this).val()+',';
allValArr.push($(this).val());
});
//allVal = allVal.substring(0, allVal.length - 1);
console.log(allValArr);
}
</script>
你可以试试这个: 演示
$('#search_rightSelected').click(function() {
var foo = [];
$('#multiple :selected').each(function(i, selected){
foo[i] = $(selected).text();
});
});
或
如果从 select 调用的 val 函数是倍数,它将返回一个数组。 $('select#search_rightSelected').val()
将返回所选选项的值数组 - 您无需自己循环访问并获取它们。