我有以下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();
}); 
有帮助吗?

解决方案 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

...现在..我怎么设置单选按钮返回到“国标”的默认?

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

感谢

其他提示

这将重置形式:

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

使用关闭事件

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

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

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

这将重置保存表单,在取消和标题栏关闭按钮。 例外SELECT2已经手动完成:

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

相同beforeClose内

`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();`
} 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top