2010-08-15 130 views
0

有沒有辦法驗證我的代碼如下?我的意思是我也有一個保存按鈕,當我點擊保存並且沒有任何複選框被選中時,我希望驗證它,但不驗證它在哪裏引發警報。複選框驗證

var chkbox= $('#divListBox').find(':checked') 
        .map(function() { 
         return $(this).val(); 
        }).get(); 

回答

0

您的意思是,當沒有選擇任何東西時,您不想提出煩人的彈出窗口?也許這樣的事情:

// if your save button is a input type="submit", then maybe 
// you would prefer to bind a 'submit' handler to the form instead 
$("#save").click(function() { 
    var chkbox= $('#divListBox').find(':checked') 
        .map(function() { 
         return $(this).val(); 
        }).get(); 
    if(chkbox.length) { 
     $("#myForm").submit(); // or however you are handling this 
    } else { 

     // populate a container with an error message 
     $("#cbError").text("Please select at least one"); 
    } 
    return false; 
}); 
+0

@ karim79 - 謝謝,我會試試看。 – hersh 2010-08-16 01:02:12