I have a table of dynamic data which is echo'ed out on the webpage in a table format as shown in the picture
LINK FOR THE IMAGE: https://ibb.co/RCYwchv
so the table will have a lot of select tags with different names and values depending upon how much (applications are there ) iteration takes place!
foreach($data['applicationList'] as $applications){
echo '<td><select name='.$applications['aid'].'>
<option value=000>Reject All<option>
<option value=111>Accept All<option>
<option value=100>Accept 1st Choise<option>
<option value=010>Accept 2nd Choise<option>
<option value=001>Accept 3rd Choise<option>
<option value=110>Accept 1st and 2nd Choise<option>
<option value=011>Accept 2nd and 3rd Choise<option>
<option value=101>Accept 1st and 3rd Choise<option>
</select></td>
}
Is there any way i could capture all the different selected values and names which are changed and bind them to an object of key value pair to send it to the controller using javascript or jquery ajax? Could someone help me on this!
I assume you wish to gather all those values each time they change...
So you nee a change
handler which will loop through all the select
elements and add their key/value to an object.
The example below should be instructive to you.
$(document).ready(function(){
// This code part is just for this demo...
// I needed to recreate multile select elements.
for(i=0;i<10;i++){
var clone = $("[name='someName']").clone();
// Setting a different name for each
var newName = clone.attr("name")+i;
clone.attr("name",newName);
$("body").append(clone);
}
// Now to get the values each time one is changing
var data_obj = {};
$("[name^='someName']").on("change",function(){
$("[name^='someName']").each(function(){
data_obj[ $(this).attr("name") ] = $(this).val();
});
// Result
console.log( JSON.stringify(data_obj) );
});
});
select{
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name='someName'>
<option value=000>Reject All<option>
<option value=111>Accept All<option>
<option value=100>Accept 1st Choise<option>
<option value=010>Accept 2nd Choise<option>
<option value=001>Accept 3rd Choise<option>
<option value=110>Accept 1st and 2nd Choise<option>
<option value=011>Accept 2nd and 3rd Choise<option>
<option value=101>Accept 1st and 3rd Choise<option>
</select>
</div>