关闭JQuery清除表单 [英] JQuery Clear Form on close

查看:158
本文介绍了关闭JQuery清除表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的JQuery Dialog脚本,我试图找出如何在关闭对话框时触发一个清除表单的函数。



$ p $ 函数clearForm()
{
$(':input','#calcQuery')
.not(':button,:submit,:reset,:hidden')
.val('');
};
//表单弹出
$(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(){

// ($ {
type:POST,
url:calc.php,
data:$(#calcQuery)。serialize() ,
dataType:html,
success:function(response)
{
$(#calcBox)。html(response);
$( (xhr,ajaxOptions,thrownError)
{
alert(xhr.status);
$,
错误:函数
b $ b aler吨(thrownError);
}



})。responseText;

//表单文章

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

});

$(#formBox)。bind('dialogclose',function(event)
{
clearForm();
});


解决方案

/ b>

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

和.....



<$ p $ b $ .ajax({
type:POST,
url:calc.php,
data:$ p $ // form post
$ .ajax $(#calcQuery)。serialize(),
dataType:html,
成功:函数(响应)
{
$(#calcBox)。html (响应);
$(#calcBox)。show();
clearForm(#calcQuery);
},
error:function
xhr,ajaxOptions,thrownError)
{
alert(xhr.status);
alert(thrownError);
}



})。responseText;

//表单后

...现在..我怎么设置单选按钮回到默认值GB?

 & nbsp; KB< input type =radio name =curr_unitvalue =KB/> 
& nbsp; MB< input type =radioname =curr_unitvalue =MB/>
& nbsp; GB< input type =radioname =curr_unitvalue =GBchecked />
& nbsp; TB< input type =radioname =curr_unitvalue =TB/>

谢谢

I have the below JQuery Dialog script, I'm trying to find out how to fire off a function that clears the form when I close the dialog.

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

解决方案

I got it to work by using ...

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

and .....

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

... now .. how do I set the radio buttons back to the default of "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" />

thanks

这篇关于关闭JQuery清除表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆