5

我有下面的 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();
}); 
4

5 回答 5

9

这将重置表单:

$("#form").trigger( "reset" );
于 2013-01-25T15:45:58.847 回答
5

使用关闭事件

$("#formBox").dialog({
      bgiframe: true,
        autoOpen: false, 
        height: 600,
        width: 400, 
        modal: false,
        close: clearForm
});
于 2009-12-07T15:30:27.907 回答
3

我让它通过使用...

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

谢谢

于 2009-12-08T19:07:08.710 回答
3

更优雅的是您的方法的组合:

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

它会在保存、取消和标题栏关闭按钮时重置您的表单。必须手动完成的 select2 例外:

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

在同一个beforeClose里面

于 2013-09-11T13:03:15.643 回答
0
`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();`
} 
于 2013-02-04T08:54:37.867 回答