Using Javascript, I am dynamically generating dropdowns list under dvContainer. I want to get selected values of all the select elements under that container. The following is HTML code generated from javascript:
<div id="dvContainer">
<div>
<select id="QType_id2">
<option value="1">MCQs</option>
<option value="2">Short Questions</option>
<option value="3">Long Questions</option>
</select>
<input type="button" value="Remove">
</div>
<div>
<select id="QType_id">
<option value="1">MCQs</option>
<option value="2">Short Questions</option>
<option value="3">Long Questions</option>
</select>
<input type="button" value="Remove">
</div>
<div>
<select id="QType_id">
<option value="1">MCQs</option>
<option value="2">Short Questions</option>
<option value="3">Long Questions</option>
</select>
<input type="button" value="Remove">
</div>
</div>
This Java Script is used to add dropdowns in dvContainer:
<script type="text/javascript">
function AddDropDownList() {
//Build an array containing Customer records.
var customers;
$.getJSON(href="URL GOES HERE",function(customers){
//Create a DropDownList element.
var ddlCustomers = $("<select />");
ddlCustomers.attr('id',"QType_id")
//Add the Options to the DropDownList.
$(customers).each(function () {
var option = $("<option />");
//Set Customer type in Text part.
option.html(this.type);
//Set id in Value part.
option.val(this.id);
//Add the Option element to DropDownList.
ddlCustomers.append(option);
});
//Reference the container DIV.
var dvContainer = $("#dvContainer")
//Add the DropDownList to DIV.
var div = $("<div />");
div.append(ddlCustomers);
//Create a Remove Button.
var btnRemove = $("<input type = 'button' value = 'Remove' />");
btnRemove.click(function () {
$(this).parent().remove();
});
//Add the Remove Buttton to DIV.
div.append(btnRemove);
//Add the DIV to the container DIV.
dvContainer.append(div);
});
};
</script>
Using AJAX/jQuery, I want to build a function, which gives selected values of all the dropdowns.
Just for the note, it is not good practice to add multiple elements with same id. Suggested is to replace ddlCustomers.attr('id',"QType_id")
with ddlCustomers.attr('name',"QType_id")
. Added below is code snippet to get all row data.
function getAllRowsData() { var alldata = $('#dvContainer>div').map(function() { var $this = $(this), data = {}; data['QType_id'] = $this.find('[name="QType_id"]').val(); return data; }).toArray(); $("#alldataop").text(JSON.stringify(alldata)); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="dvContainer"> <div> <select name="QType_id"> <option value="1">MCQs</option> <option value="2">Short Questions</option> <option value="3">Long Questions</option> </select> <input type="button" value="Remove"> </div> <div> <select name="QType_id"> <option value="1">MCQs</option> <option value="2">Short Questions</option> <option value="3">Long Questions</option> </select> <input type="button" value="Remove"> </div> <div> <select name="QType_id"> <option value="1">MCQs</option> <option value="2">Short Questions</option> <option value="3">Long Questions</option> </select> <input type="button" value="Remove"> </div> </div> <input type="button" onclick="getAllRowsData()" value="Get Data"> <div id="alldataop"></div>
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.