how can get the value selected by the user on each select field using $(.promote).each(function(){}); the html code is:
<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>;
You can use a button and on click you get the values of each select
using .map()
$("#getValues").on("click", function() { var values = $(".promote").map(function() { return $(this).val(); }).get(); alert(values); });
<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" />
You can use .each() too:
$("#getValues").on("click", function () { var values = []; $(".promote").each(function (i) { values[i] = $(this).val(); }); alert(values); });
<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" />
The each method is meant to be an immutable iterator, where as the map method can be used as an iterator, but is really meant to manipulate the supplied array and return a new array.
Another important thing to note is that the each function returns the original array while the map function returns a new array. If you overuse the return value of the map function you can potentially waste a lot of memory.
$('.promte').each(function(){
// this.value;
// OR
// $(this).val();
})
var count = 0;
var array = [];
$('.promote').each(function(){
array[count] = $(this).val();
count++;
});
$(".promote").each(function() {
var $selectBox = $(this);
var selectedOption = $selectBox.find(":selected").val();
});
JSFiddle: http://jsfiddle.net/acuocgt5/
instead of each method, you can directly get the values as an array
var selValues = $('.promote').map(function(){return $(this).val()}).get()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.