2009-12-07 41 views
5

Tôi có tập lệnh Hộp thoại JQuery bên dưới, tôi đang cố gắng tìm hiểu cách kích hoạt một hàm xóa biểu mẫu khi tôi đóng hộp thoại.Biểu mẫu xóa JQuery trên đóng

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() có thể làm các trick ... – NDM

Trả lời

3

tôi đã nhận nó để làm việc bằng cách sử dụng ...

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

và .....

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

... bây giờ .. làm cách nào để đặt các nút radio trở về mặc định là "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" /> 

nhờ

5

Sử dụng close event

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

này sẽ thiết lập lại các hình thức:

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

Bạn có thể làm rõ nơi dòng này nên đi. Cảm ơn. –

+0

@FrancisRodgers Nhìn vào câu trả lời của tôi –

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

nơi nó vào chức năng thành công sau thông báo thành công đã được hiển thị.
Sau đó, nó hoạt động hoàn hảo!
dụ:

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

tao nhã hơn là một sự kết hợp các phương pháp của bạn:

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

Nó sẽ thiết lập lại hình thức của bạn về tiết kiệm, về hủy và trên nút đóng thanh tiêu đề. ngoại lệ cho Select2 mà phải được thực hiện bằng tay:

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

bên trong BeforeClose cùng