2009-12-07 15 views
5

Tengo el siguiente script de JQuery Dialog, estoy tratando de averiguar cómo activar una función que borra el formulario cuando cierro el diálogo.JQuery Clear Form en el cierre

function clearForm() 
{ 
$(':input','#calcQuery') 
.not(':button, :submit, :reset, :hidden') 
.val(''); 
}; 
// form popup 
$(document).ready(function() 
{ 
//var dataString = $("#calcQuery").serialize(); 
    $("#formBox").dialog({ 
     bgiframe: true, 
     autoOpen: false, 
     height: 600, 
     width: 400, 
     modal: false, 
     closeOnEscape: true, 
     title: "Calculator", 
     buttons: { 
      "Calculate": function() { 

// form post 
      $.ajax({ 
      type: "POST", 
      url: "calc.php", 
      data: $("#calcQuery").serialize(), 
      dataType: "html", 
      success: function(response) 
       { 
       $("#calcBox").html(response); 
       $("#calcBox").show(); 
       }, 
      error: function 
       (xhr, ajaxOptions, thrownError) 
        { 
        alert(xhr.status); 
        alert(thrownError); 
        } 



    }).responseText; 

// form post 

       } 
      } 
    }); 

$('#calcButton').click(function(){ 
    $('#formBox').dialog('open'); 
    return false; 
    }); 

}); 

$("#formBox").bind('dialogclose', function(event) 
{ 
clearForm(); 
}); 
+0

form.reset() puede hacer el truco ... – NDM

Respuesta

3

Tengo que funcione mediante el uso de ...

function clearForm(form) 
{ 
    $(":input", form).each(function() 
    { 
    var type = this.type; 
    var tag = this.tagName.toLowerCase(); 
     if (type == 'text') 
     { 
     this.value = ""; 
     } 
    }); 
}; 

y .....

// form post 
      $.ajax({ 
      type: "POST", 
      url: "calc.php", 
      data: $("#calcQuery").serialize(), 
      dataType: "html", 
      success: function(response) 
       { 
       $("#calcBox").html(response); 
       $("#calcBox").show(); 
       clearForm("#calcQuery"); 
       }, 
      error: function 
       (xhr, ajaxOptions, thrownError) 
        { 
        alert(xhr.status); 
        alert(thrownError); 
        } 



    }).responseText; 

// form post 

... ahora ... ¿cómo restablezco los botones de radio al valor predeterminado de "GB"?

&nbsp;KB <input type="radio" name="curr_unit" value="KB" /> 
&nbsp;MB <input type="radio" name="curr_unit" value="MB" /> 
&nbsp;GB <input type="radio" name="curr_unit" value="GB" checked/> 
&nbsp;TB <input type="radio" name="curr_unit" value="TB" /> 

gracias

5

Utilice la close event

$("#formBox").dialog({ 
     bgiframe: true, 
     autoOpen: false, 
     height: 600, 
     width: 400, 
     modal: false, 
     close: clearForm 
}); 
6

Esto restablecerá la forma:

$("#form").trigger("reset"); 
+0

¿Puede aclarar dónde esta línea debe ir. Gracias. –

+0

@FrancisRodgers Mira mi respuesta –

0
`jQuery("#form").trigger("reset");` 

colocarlo en la función éxito después de se haya mostrado el mensaje de éxito.
¡Entonces funciona perfectamente!
ejemplo:

success : function(){ 
jQuery('#contactsMsgs').html('<p class="success">All is well, your e&ndash;mail has been sent.</p>'); 
jQuery('#yourformname').trigger('reset'); 
spinner.hide();` 
} 
3

más elegante es una combinación de los métodos:

... 
beforeClose: function() { 
    $("#form").trigger("reset"); 
}, 
... 

se restablecerá su forma de ahorrar, de cancelación y el botón de cierre barra de título. Excepción para select2 que tienen que ser hecho manualmente:

$("#mySelect2").select2('data', null); 

dentro de la misma BeforeClose