help Input select Dynamic with ajax and jquery

0

I am new to programming, and I need your support please:

By default if you load the data, but I need the client to select this input before saving, it should show "Select", and so the client is obliged to select the input (), choose and save.

HTML:

    <div class="form-group"> 
<label for=" codDist'">Distrito</label> 
<select class="form-control " id=" codDist'" placeholder="Enter username"></select> 
</div>

JS:

    function cargaDistrito(depart, provin) {
        var $select = $('#codDist');
        $.ajax({data : {
                coddepartamento : depart,
                codprovincia : provin
                    },
                    url : "DistritPais",
                    type : "GET",
                    dataType : 'JSON',
                    success : function(data) {
                        $select.html('');
                        $.each(data,
                        function(key, val) {
        $select.append('<option value="' + val.codDist + '">'+val.descrip+'</option>');})
                    },
                    error : function() {
                        $select.html('<option id="-1">Cargando...</option>');
                    }
                });
    }
    
asked by APRECode 22.05.2018 в 03:09
source

2 answers

0

Thanks for your answers and maybe someone needs the code, corrected it would end like this:

function cargaDistrito(depart, provin) {
    var $select = $('#codDist');
    $.ajax({data : {
            coddepart : depart,
            codprovin : provin
                },
                url : "DistritPais",
                type : "GET",
                dataType : 'JSON',
                success : function(data) {
                    $select.html('');

$ select.append ('SELECT ...

                    $.each(data, function(key, val) {
    $select.append('<option value="' + val.codDist + '">'+val.descrip+'</option>');})
                },
                error : function() {
                    $select.html('<option id="-1">Cargando...</option>');
                }
            });
}
    
answered by 22.05.2018 / 23:48
source
0

Before doing the tour with each you must add an append ("Select") and then your each one so that you can display all the data that you bring by ajax!

Greetings

    
answered by 22.05.2018 в 13:52