关闭JQuery Clear Form

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

这将重置表单:

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

使用close事件

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

我用它来工作……

 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”?

  KB   MB   GB   TB  

谢谢

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

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

它将在保存,取消和标题栏关闭按钮上重置您的表单。 select2的例外情况必须手动完成:

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

在同一个之前关闭

 `jQuery("#form").trigger( "reset" );` 

在显示成功消息后将其置于成功function中。
然后它完美地运作!
例:

 success : function(){ jQuery('#contactsMsgs').html('

All is well, your e–mail has been sent.

'); jQuery('#yourformname').trigger( 'reset' ); spinner.hide();` }
Interesting Posts