個人的には結構使用する動作なので紹介いたします。
プライバシーポリシーや20歳以上の確認を行うときなどに有効ですね。
残念ながらフォームのスパム対策としてはおそらく効果は無いと思われます。
step1:javascript読み込みます
[javascript]
<script type="text/javascript" src="jqueryまでのパス/jquery.js"></script>
<script type="text/javascript">// <![CDATA[
$(function() {
$("form").submit(function() {
var checkbox_val = $("input:checked[name^=private]").val(); //nameを合わせる(*1)
if (checkbox_val) {
return true;
} else {
//$("#priv_error").slideDown();
alert("チェックボックスにチェックを入れてください。");
return false;
}
});
});
// ]]></script>
[/javascript]
step2:HTMLの記述を以下のようにします
[html]
<form action="next." method="post"><!– nameを合わせます。(*1) –>
<input id="private" type="checkbox" name="private" />チェック入れたらサブミットできます。
<input id="submit" onmouseover="this.src=’./images/bt_on.gif’" onmouseout="this.src=’./images/bt.gif’" type="image" alt="" height="60" name="submit" src="./images/bt.gif" width="200" />
</form>
[/html]
inputのnameとjavascriptのソース内のname要素を合わせなければ動作しません。
参考サイト
http://www.omnioo.com/record/javascriptjquery/check_and_submit/