Pergunta

Eu tenho o abaixo JQuery diálogo roteiro, eu estou tentando descobrir como disparar uma função que cancela a forma quando eu fechar o diálogo.

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();
}); 
Foi útil?

Solução 3

Eu tenho que trabalhar usando ...

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

e .....

// 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

... agora .. Como faço para configurar os botões de rádio de volta ao padrão 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" />

graças

Outras dicas

Isso irá redefinir a forma:

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

Use a próximo evento

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

Mais elegante é uma combinação de seus métodos:

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

Ele irá redefinir a sua forma de salvar, em cancelar e na barra de título botão de fechar. Exceção para select2 que tem que ser feito manualmente:

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

dentro do mesmo BeforeClose

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

colocá-lo na função de sucesso depois a mensagem de sucesso foi exibido.
Em seguida, ele funciona perfeitamente!
exemplo:

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();`
} 
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top