如何使用$(。promote).each(function(){})获取用户在每个选择字段上选择的值; html代码是:
<div class="class1">
<select class=" promote">
<option value="Promote">Promote</option>
<option value="DoNotPromote" selected="selected">Do not Promote</option>
</select>
</div>
<div class="class2">
<select class=" promote">
<option value="Promote">Promote</option>
<option value="DoNotPromote" selected="selected">Do not Promote</option>
</select>
</div>;
<div class="class3">
<select class=" promote">
<option value="Promote">Promote</option>
<option value="DoNotPromote" selected="selected">Do not Promote</option>
</select>
</div>;
<div class="class4">
<select class=" promote">
<option value="Promote">Promote</option>
<option value="DoNotPromote" selected="selected">Do not Promote</option>
</select>
</div>;
答案 0 :(得分:3)
您可以使用按钮,点按即可使用.map()
获取每个select
的值
$("#getValues").on("click", function() {
var values = $(".promote").map(function() {
return $(this).val();
}).get();
alert(values);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="class1">
<select class=" promote">
<option value="Promote">Promote</option>
<option value="DoNotPromote" selected="selected">Do not Promote</option>
</select>
</div>
<div class="class2">
<select class=" promote">
<option value="Promote">Promote</option>
<option value="DoNotPromote" selected="selected">Do not Promote</option>
</select>
</div>
<div class="class3">
<select class=" promote">
<option value="Promote">Promote</option>
<option value="DoNotPromote" selected="selected">Do not Promote</option>
</select>
</div>
<div class="class4">
<select class=" promote">
<option value="Promote">Promote</option>
<option value="DoNotPromote" selected="selected">Do not Promote</option>
</select>
</div>
<input type="button" value="Get All Values" id="getValues" />
&#13;
您也可以使用.each():
$("#getValues").on("click", function () {
var values = [];
$(".promote").each(function (i) {
values[i] = $(this).val();
});
alert(values);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="class1">
<select class=" promote">
<option value="Promote">Promote</option>
<option value="DoNotPromote" selected="selected">Do not Promote</option>
</select>
</div>
<div class="class2">
<select class=" promote">
<option value="Promote">Promote</option>
<option value="DoNotPromote" selected="selected">Do not Promote</option>
</select>
</div>
<div class="class3">
<select class=" promote">
<option value="Promote">Promote</option>
<option value="DoNotPromote" selected="selected">Do not Promote</option>
</select>
</div>
<div class="class4">
<select class=" promote">
<option value="Promote">Promote</option>
<option value="DoNotPromote" selected="selected">Do not Promote</option>
</select>
</div>
<input type="button" value="Get All Values" id="getValues" />
&#13;
每个方法都是一个不可变的迭代器,其中 map方法可以用作迭代器,但实际上是有意义的 操纵提供的数组并返回一个新数组。
另一个需要注意的重要事项是每个函数返回 原始数组,而map函数返回一个新数组。如果你 过度使用可能浪费的地图功能的返回值 很多记忆。
答案 1 :(得分:0)
$('.promte').each(function(){
// this.value;
// OR
// $(this).val();
})
答案 2 :(得分:0)
var count = 0;
var array = [];
$('.promote').each(function(){
array[count] = $(this).val();
count++;
});
答案 3 :(得分:0)
$(".promote").each(function() {
var $selectBox = $(this);
var selectedOption = $selectBox.find(":selected").val();
});
JSFiddle:http://jsfiddle.net/acuocgt5/
答案 4 :(得分:0)
代替每个方法,您可以直接将值作为数组
获取var selValues = $('.promote').map(function(){return $(this).val()}).get()