代码之家  ›  专栏  ›  技术社区  ›  Simply Seth

jquery关闭时清除窗体

  •  5
  • Simply Seth  · 技术社区  · 15 年前

    我有下面的jquery对话框脚本,我正在试图找出如何在关闭对话框时触发一个清除窗体的函数。

    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();
    }); 
    
    5 回复  |  直到 11 年前
        1
  •  7
  •   WEFX venkateswararao    12 年前

    这将重置表单:

    $("#form").trigger( "reset" );
    
        2
  •  5
  •   PetersenDidIt    15 年前

    使用 close event

    $("#formBox").dialog({
          bgiframe: true,
            autoOpen: false, 
            height: 600,
            width: 400, 
            modal: false,
            close: clearForm
    });
    
        3
  •  3
  •   Simply Seth    15 年前

    我用…

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

    还有…

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

    ……现在。。如何将单选按钮设置回默认的“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" />
    

    谢谢

        4
  •  3
  •   Adrian P.    11 年前

    更优雅的是你的方法的结合:

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

    它将在保存、取消和标题栏关闭按钮上重置表单。 必须手动执行的Select2异常:

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

    在关闭之前在同一个内部

        5
  •  0
  •   doris pelger    12 年前
    `jQuery("#form").trigger( "reset" );`
    

    将其放入成功功能 之后 已显示成功消息。
    那就完美了!
    例子:

    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();`
    }