I am retrieving the data from database as options into my dropdown and I am having an add button which will add the dropdowns here I am getting options from database so there are two options so every dropdown which I add should have two options here the problem is
//Select option function $(document).on("change", ".drop", function() { //Enable button on selection $('#seedoc').prop('disabled', false) }) //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();
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" type='text/css' rel='stylesheet' /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="container" class=""></div> <i class="fa fa-plus-square fa-2x" aria-hidden="true" id="seedoc" style="float: right; margin-right: 10px;" onclick="addOne();" disabled></i> <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>
Please help me anyone thanks in advance!
You are appending the <option>
tags every time you load new items from database. Simply clean dropdowns first, or replace the ones you already have with new ones:
function getEmails() {
$('.drop').html('');
res.users.forEach(function(option) {
$('.drop').append('<option data-id=' + $(option).attr('data-id') + '>' + option + '</option>');
});
}
OR
function getEmails() {
let options = '';
res.users.forEach(function(option) {
options += '<option data-id=' + $(option).attr('data-id') + '>' + option + '</option>';
});
$('.drop').html(options);
}
Im not sure if icon can be disabled. If you dont want to wrap it with button, simply add condition in the event listener. First, instead of "disabled" set data-enabled="0"
and removing disabled property and the "onclick=addOne()"
call:
<i class="fa fa-plus-square fa-2x" aria-hidden="true" id="seedoc" style="float: right; margin-right: 10px;" data-enabled="0"></i>
Than, register action for icon click and add condition in it:
$(document).on("change", ".drop", function() {
$('#seedoc').data().enabled = 1; //set data-enabled to true instead of disabled = false
});
$(document).on("click", "#seedoc", function(e) {
if ($(this).data().enabled) addOne();
});
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.