2011-01-11 12 views
0

私は、フォームの表形式で表示される一連のレコードを持っています。各レコードのDeleteチェックボックスがあります - ここでフォームが簡略化された形式である:削除の確認

<form method="post" action="" id="update-history-form"> 
    Item 1 <input type="checkbox" value="1" name="History[0][delete]"> 
    Item 2 <input type="checkbox" value="1" name="History[1][delete]"> 
    Item 3 <input type="checkbox" value="1" name="History[2][delete]"> 

    <input type="submit" value="Update History" name="update"> 
</form> 

入力の「name」属性の整数値は、削除のために選択されたレコードを識別するのに役立ちます。

私が欲しいのは、削除チェックボックスのいずれかにチェックマークが付いていれば(送信時に)JavaScriptアラート確認が表示されます。

答えて

2
$('#update-history-form').submit(function(){ 
    if ($(this).find('input:checkbox:checked').length){ 
    return confirm("Really delete any of them?"); 
    } 
}); 

これは、ユーザーのフォーム提出をキャンセルします。確認ダイアログボックスではありません。

フォームに非削除チェックボックスがある場合は、セレクタを名前がcontains「削除」などの入力に変更する必要があります。 jQueryのを使用して

$(this).find('input[name*="delete"]:checked') 
+0

おかげで、これはpefrectに動作します。すぐに回答を受け入れます。 – GSTAR

0

$('#update-history-form').submit(function(ev) { 
    ev.preventDefault(); 
    if (this.find("input:checkbox:checked").length == 0 || confirm("Are you sure?")) this.submit(); 
}); 
0
<form method="post" action="" id="update-history-form" onsubmit='return confirmChecks(this);'> 
    Item 1 <input type="checkbox" value="1" name="History[0][delete]"> 
    Item 2 <input type="checkbox" value="1" name="History[1][delete]"> 
    Item 3 <input type="checkbox" value="1" name="History[2][delete]"> 

    <input type="submit" value="Update History" name="update"> 
</form> 
<script type='text/javascript'> 
function confirmChecks(someForm) { 
    var inputList = someForm.getElementsByTagName('input'); 
    var aCheckboxIsChecked = false; 
    for (var i=0; i < inputList.length; i++) { 
    if (inputList[i].type.toLowerCase() == 'checkbox' && inputList[i].checked) { 
     aCheckboxIsChecked = true; 
     break; 
    } 
    } 

    if (aCheckboxIsChecked) { 
    var proceed = confirm('Really delete those things?'); 
    if (!proceed) { 
     return false; 
    } 
    } 
    return true; 
} 
</script> 
関連する問題