请问,jQuery怎么获取多选下拉框值?
.val()?
遍历?
直接获取就是所有的
var data = $('select[name="travel[]"]').map(function () {
return $(this).val();
}).get();
console.log(data);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Title</title>
<style>
label {
font-family: sans-serif;
}
label {
font-size: 1rem;
padding-right: 10px;
}
select {
font-size: 0.9rem;
padding: 2px 5px;
}
</style>
</head>
<body>
<label for="pet-select">Choose a pet:</label>
<select name="pets" id="select" multiple>
<option value="">--Please choose an option--</option>
<option value="dog">Dog</option>
<option value="cat">Cat</option>
<option value="hamster">Hamster</option>
</select>
<button onclick="clickEvent()">获取select多选值</button>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
function clickEvent() {
console.log("select", $("#select").val()); // 数组
}
</script>
</body>
</html>