2010-05-21 33 views
8

Tôi đang cố gắng để làm một hộp thoại xác nhận sử dụng jquery, nhưng hình thức không được gửi ở tất cả, đây là những gì tôi nhận:thoại jquery: khẳng định nhấp chuột vào một nút gửi

<script type="text/javascript"> 
    var currentForm; 
    $(function() { 
     $("#dialog-confirm").dialog({ 
      resizable: false, 
      height: 140, 
      modal: true, 
      autoOpen: false, 
      buttons: { 
       'Delete all items': function() { 
        currentForm.submit(); 
        $(this).dialog('close'); 
       }, 
       Cancel: function() { 
        $(this).dialog('close'); 
       } 
      } 
     }); 
    }); 

    function ask() { 
     currentForm = $(this).closest('form'); 
     $("#dialog-confirm").dialog('open'); 
    } 
</script> 
<form ... > 
    <input type="submit" value="delete" onclick="ask();return false;" /> 
</form> 

Trả lời

16

bạn cần có nút thoại của bạn nộp <form>, như thế này:

   'Delete all items': function() { 
        $(this).dialog('close'); 
        $("#myForm").submit(); 
       } 

Phần còn lại của mã của bạn là chính xác, nhưng hiện tại nó chỉ đóng hộp thoại và quay lại, bạn cần phải thực sự gửi biểu mẫu. Ngoài ra, nó tốt hơn để làm điều này như một handler nhấp chuột, thay vì onclick, như thế này (cập nhật để lấy ý kiến ​​):

var currentForm; 
    $(function() { 
     $("#dialog-confirm").dialog({ 
      resizable: false, 
      height: 140, 
      modal: true, 
      autoOpen: false, 
      buttons: { 
       'Delete all items': function() { 
        $(this).dialog('close'); 
        currentForm.submit(); 
       }, 
       'Cancel': function() { 
        $(this).dialog('close'); 
       } 
      } 
     }); 
     $(".delete").click(function() { 
      currentForm = $(this).closest('form'); 
      $("#dialog-confirm").dialog('open'); 
      return false; 
     }); 
    }); 

Sau đó chỉ cần cung cấp cho <input> của bạn một lớp của delete, như thế này:

<input class="delete" type="submit" value="delete" /> 
+1

vấn đề là tôi có rất nhiều biểu mẫu, mỗi nút có nút xóa bên trong nó, nó chỉ là một đầu vào bị ẩn ' 'tạo nên sự khác biệt giữa các hình thức này – Omu

+0

Tôi có thể cung cấp các biểu mẫu tên/id khác nhau, tên có thể chứa biểu mẫu + theId – Omu

+1

@Omu - Bạn có thể lưu trữ một" currentForm = '$ (this) .closest ('form') 'trong hàm click khi nó mở ra, sau đó chỉ cần gọi' currentForm.submit() 'trong trình xử lý của nút delete, không cần ID ... và chỉ cần thay đổi trình xử lý xóa để sử dụng bộ chọn lớp hoặc thuộc tính. –

4

Nếu bạn đang sử dụng jQuery, sau đó làm điều đó đúng và tránh inline Javascript:

$(function(){ 
    $("form").submit(function(){ 
    // return false if you don't want this to be submitted 
    // maybe do a 
    // return ask(); 
    // but with the code provided it doesn't seem 
    // to work like that - ask() is not returning anything at all! 
    }); 
}); 
0

này đã làm việc cho tôi:

$(function() { 
    $("#accordion").accordion({ 
     heightStyle: "content" 
    }); 

    function confirmDialog(title, message, success) { 
     var confirmdialog = $('<div></div>').appendTo('body') 
      .html('<div><h6>' + message + '</h6></div>') 
      .dialog({ 
       modal: true, 
       title: title, 
       zIndex: 10000, 
       autoOpen: false, 
       width: 'auto', 
       resizable: false, 
       buttons: { 
        Yes: function() { 
         success(); 
         $(this).dialog("close"); 
        }, 
        No: function() { 
         $(this).dialog("close"); 
        } 
       }, 
       close: function() { 
        $(this).remove(); 
       } 
      }); 

     return confirmdialog.dialog("open"); 
    } 

    $('form').on('submit', function (e) { 
     e.preventDefault(); 
     var form = this; 

     confirmDialog('Confirm', 'Are you sure?', function() { 
      form.submit(); 
     }); 
    }); 
}); 

tham khảo: http://jsfiddle.net/pmw57/67Ge2/

Các vấn đề liên quan