2012-01-31 88 views
1
$('#delete').click(function (e) { 
    e.preventDefault(); 
    var result = true; 
    if($('.checkbox:checked').length < 1) { 
     $.notifyBar({ 
      cls: "error", 
      html: 'At least 1 checkbox must be checked.', 
      delay: 5000 
     }); 
     result = false; 
    } 
    return result; 
}); 

問題是按鈕沒有提交表格如果一切順利 有什麼問題嗎?點擊功能問題

+0

請鏈接到一個完整的JSFiddle,以便重現問題。 – Domenic 2012-01-31 16:49:14

+4

請正確縮進您的代碼。 「縮進你的代碼」和「生成有效的代碼」直接相關。 *(@ JamWaffles這次爲你做了,下次請自己做)* – Tomalak 2012-01-31 16:49:35

+0

刪除e.preventDefault(),它會提交你的表單,如果爲false,它會顯示notifyBar – 2012-01-31 16:50:35

回答

2

e.preventDefault()的調用阻止了提交表單的按鈕的默認操作。只有當你不想要提交時才需要調用它。

$('#delete').click(function(e) { 
    if($('.checkbox:checked').length<1){ 
    $.notifyBar({ 
     cls: "error", 
     html: 'At least 1 checkbox must be checked.', 
     delay: 5000 
    }); 

    // Invalid, stop the submit 
    e.preventDefault(); 
    } 
}) 
0

使用e.preventDefault();將停止提交表單,即使返回true也是如此。

1

我想這是你想要做什麼:

$('#delete').click(function (e) { 
    var result = true; 

    if($('.checkbox:checked').length < 1) { 
     $.notifyBar({ 
      cls: "error", 
      html: 'At least 1 checkbox must be checked.', 
      delay: 5000 
     }); 
     result = false; 
    } 

    if(!result) e.preventDefault(); 
}); 

e.preventDefault();呼叫將阻止提交按鈕提交從不管你從函數返回什麼。在我的編輯中,只有結果爲false時,纔會阻止默認操作。

0

你應該把它放在onsubmit處理程序中,而不是點擊處理程序。另外,請確保您希望它們點擊的按鈕屬於提交類型:

function doSubmit() 
{ 
    var result = true; 

    if($('.checkbox:checked').length < 1) { 
     $.notifyBar({ 
      cls: "error", 
      html: 'At least 1 checkbox must be checked.', 
      delay: 5000 
     }); 
     result = false; 
    } 

    return result; 
    } 


<form onsubmit="return doSubmit()"> 

    Checkboxes.... 

    <input type="submit" value="submit" /> 
</form>