update select field using ajax

1

I'm starting to use codeigniter, javascript with the jquery library and I have the following problem, I have a form and in the select field where I load the cities I send the data from my controller in json, from the sgt form.

function LlenarCombo(){
       $.post('http://localhost/app/ciudad/getCiudades', 
       function (data){
          var c = JSON.parse(data);
          $.each(c, function(i, item){
                $('#ciudad').append('<option value="'+item.Id_ciudad+'">'+item.Nombre+'</option>');
          });
       });
    }

The fact is that next to the select I have a button with the option to add a new city, the metedo works well and keeps the data well, my problem is that I need to save the select field updated without reloading the page.

function guardarCiudad(){
      $.ajax({
        url : 'http://localhost/app/ciudad/Guardar',
        type: "POST",
        data: $('#formCiudad').serialize(),
        success: function(data)
        {
           $('#modalCiudad').modal('hide');
           alert(data);
           LlenarCombo();
           $("#formCiudad")[0].reset();
        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert('Error al guardar los datos');
        }
      });
    }

When calling the function in the succes, obiemente repeats me the data, what else can I do?

    
asked by FeRcHo 12.05.2017 в 21:16
source

1 answer

1

I give you 2 ocpiones

1: clean city ID with:

$("#ciudad").empty();

2: Kill all the events you have running:

$('#ciudad').unbind();
    
answered by 12.05.2017 в 22:36