Hi I am new to jquery actually I had an dropdown in my html where I will get the options from database using ajax here I am retrieving the id and option from database but showing only the options in the dropdown
The code for dropdown which I done so far in html is
<div id="container" class=""></div>
<button id="seedoc" style="float: right; margin-right: 10px;" onclick="addOne();"></button>
function addOne() {
getEmails();
$('#container').append(
<div class='form-group' style='display:flex'>
<select class='drop form-control' name='option' id='option'>
<option value='' disabled selected>Select your option</option>
</select>
<button class="btn btn-primary shrBtn" style="float:left;" onclick="send()">Send</button>
</div>
the json response I get from db.php is
users:
[ "<p style='margin:0px;display:none;'data-id='755'>amilham</p>",
"<p style='margin:0px;display:none;'data-id='706'>a_sarabi</p>"]
and in dropdown I am getting amilham,a_sarabi so here in the dropdown if I select amilham I need to get the data-id ie 755 into an array and also in the another dropdown if I select a_sarabi I need to get 706 into array using jquery
can anyone help me please
so far I tried like this but not getting
function send()
{
var uids = [];
\$('.drop').change(function(){
\$(this).find('option:selected').each(function(){
uids.push(\$(this).attr('data-id') );
});
});
I need all the ids of selected options in all the dropdowns I add using jquery Populating options from db into dropdown
function getEmails(){
\$.ajax({
url :'propage.php',
type : 'POST',
dataType : 'json',
data : '&userId='+\$('#hdn_userid').val()+'&action=getEmails',
success : function(res){
res.users.forEach(function(option) {
\$('.drop').append('<option>' + option + '</option>');
});
},
});
}
I have created a demo example (by adding your json
response data form db.php) of what you are saying. You can store the data-id
on clicking send button.
Run snippet below and select the option and click send to see the array uids
will have the data-id
of the selected option.
//Store in array() var uids = []; //Select option function $(document).on("change", ".drop", function() { //Find option selected var data = $(this).find("option:selected").attr('data-id') //Push selected data-id uids.push(data) //Enable button on selection $('#seedoc').prop('disabled', false) }) //Send function send() { console.log(uids) } //response var res = { "users": ["<p style='margin:0px;display:none;'data-id='755'>amilham</p>", "<p style='margin:0px;display:none;'data-id='706'>a_sarabi</p>" ] } function getEmails() { res.users.forEach(function(option) { $('.drop').append('<option data-id=' + $(option).attr('data-id') + '>' + option + '</option>'); }); } function addOne() { $('#container').append("<div class='form-group' style='display:flex'><select class='drop form-control' name='option' id='option'> <option value='' disabled selected>Select your option</option> </select>"); getEmails(); } getEmails();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="container" class=""></div> <button id="seedoc" style="float: right; margin-right: 10px;" onclick="addOne();" disabled>Add one </button> <div class='form-group' style='display:flex'> <select class='drop form-control' name='option' id='option'> <option value='' disabled selected>Select your option</option> </select> <button class="btn btn-primary shrBtn" style="float:left;" onclick="send()">Send</button> </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.