You will see, I have a select label and next to it a button to which I add new items to it, the problem is that the option that I add does not select me.
I have tried to print data and it does not show me anything, it is empty, I do not know if this is the problem.
Everything else works correctly, it sends me the data to the url, etc.
HTML
<select class="form-control select select2-hidden-accessible" id="modelo-add" tabindex="-1">
<option></option>
JS
$(".select").select2({
placeholder: "Seleccionar.."});
var url = "includes/lists/adds/add-list-modelos.php";
var dataValue = {'marca': marca, 'modelo': modelo};
$.ajax({
type: "POST",
url: url,
data: dataValue,
cache: false,
async: true,
success: function (data) {
setTimeout(function () {
$('#modelo-add').append('<option value="'+data+'">'+modelo+' '+marca+'</option>');
$('#modelo-add').val(data);
}, 200);
}
});
The failure would be when you put it
value = "' + data + '"
since itdata
is an object. You should specify which property to add:Marca
orModelo
. And the same when assigning theval()
And after assigning the value, add a
change()
to select that option: