2009-12-07 73 views
5

我有下面的JQuery对话框脚本,我试图找出如何在关闭对话框时清除表单的函数。关闭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(); 
}); 
+0

form.reset()可能做的伎俩... – NDM 2009-12-07 15:30:02

回答

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

感谢

5

使用close event

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

这将重置形式:

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

你能澄清这条线应该去。谢谢。 – 2013-01-25 16:14:21

+0

@FrancisRodgers看看我的答案 – 2013-09-11 13:08:06

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

更优雅是你方法的组合:

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

这将重置保存形式,在取消和标题栏关闭按钮。 例外的选择2已经手动完成:

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

相同beforeClose内