jquery - After reset fields with .val(''), the form fields are empty until the page will be reloaded -


in document use bootstrap modal load document dynamically. in modal window use ajax add value of 2 form fields database. after request reset 2 form filed .val(''). works fine when i'm close modal window , open again, every time form field empty. when i'm reload page , open modal, works fine.

where error? reset of 2 form field whole session until i'm reload page.

here ajax code:

        $(document).on('click', 'form.form-inline button', function(e) {          e.preventdefault ? e.preventdefault() : e.returnvalue = false;          var pmacaddr = $('#macaddr').val();         var phostname = $('#hostname').val();          var = $(this);          $.ajax({             async: true,             type: 'post',             url: propertie.backend+'?action=ajax&method=addmac&lang='+propertie.language+'&code='+math.random(),             datatype: 'json',             data: {                 'hostname': phostname,                 'macaddr': pmacaddr,             },error: function(data) {                 alert('php-error: '+data['responsetext']);             },success: function(data) {                 if (data.success === false) {                     alert(data.message);                 } else {                      var html = "<tr id=\""+pid+"\">\n";                         html += "\t<td>"+pmacaddr+"</td>\n";                         html += "\t<td>"+phostname+"</td>\n";                         html += "\t<td><a href=\"javascript:void(0);\" title=\""+propertie.l10n.delete+"\">";                         html += "<i class=\"fa fa-close fa-fw\"></i></a><a href=\"javascript:void(0);\"";                         html += " title=\""+propertie.l10n.edit+"\"><i class=\"fa fa-pencil fa-fw\"></i></a></td>\n</tr>\n";                          $(that).closest("tr").before(html);                          $('#macaddr').val('');                         $('#hostname').val('');                  }             },cache: false,         });      }); 

to open modal use code:

        $(document).on('click', '[data-toggle="ajaxmodal"]', function(e) {         $('#ajaxmodal').removedata('bs.modal');         e.preventdefault ? e.preventdefault() : e.returnvalue = false;         var $this = $(this)         , $remote = $this.data('remote') || $this.attr('href')         , $modal = $('<div class="modal fade" id="ajaxmodal"><div class="modal-dialog"><div class="modal-content"></div></div></div>');         $('body').append($modal);         $modal.modal({remote: $remote});         $modal.modal('show');     }); 

i have found mistake. modal element hidden, not removed document. have modify open-modal function , modal window removed document when close modal.

        // open modal     $(document).on('click', '[data-toggle="ajaxmodal"]', function(e) {          e.preventdefault ? e.preventdefault() : e.returnvalue = false;          var $this = $(this)         , $id = $this.data('id') || 'ajaxmodal'         , $remote = $this.data('remote') || $this.attr('href')         , $modal = $('<div class="modal fade" id="'+$id+'" role="dialog"><div class="modal-dialog"><div class="modal-content"></div></div></div>');          $('body').append($modal);         $modal.modal({remote: $remote, keyboard: false, backdrop: false});         $modal.modal('show');          // on hidden event, remove current modal document         $(document).on('hidden.bs.modal', '#'+$id, function(e) {             $(this).remove();         });      }); 

and form fields works val().


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -