dポイントプレゼントキャンペーン実施中!

探して見つけた↓をWordPressで使わせていただきたいのですが、
WordPressの投稿ページで動きません・・・。

<html>
<head>
<script type="text/javascript">

function goServlet(url){
if (!document.chbox.cBox.checked){
// 同意してない
window.alert("同意して");
}else{
// 同意してる
document.location = url;
}
}
</script>

</head>
<body>
<p>上記、「システム利用規約」に同意します。
<form name="chbox">
<input type="checkbox" name="cBox" >
</form>
</p>
<a href="javascript:goServlet('http://google.com')" >トップページへはこちら</a>
<a href="javascript:goServlet('http://yahoo.jp')" >会員ページへはこちら</a>
</body>
</html>

「"javascript:goServlet('http://google.com')"
この部分がダメなのだと思うのですが、他の方法を教えていただけないでしょうか・・・。
できましたらformのsubmitで・・・。
(すみませんド素人です)

よろしくお願いします。

A 回答 (2件)

IE9 以上なら



<script>
addEventListener('DOMContentLoaded', function(){
var checkAgree = function(ev) {
var agreed = document.forms['chbox'].elements['cBox'].checked;
if (!agreed) {
ev.preventDefault();
alert('同意して');
}
}
var Q = function(s){return Array.prototype.slice.apply(document.querySelectorAll(s))};
Q('a.agreed').forEach(function(a){a.addEventListener('click', checkAgree, false)});
Q('form.agreed').forEach(function(f){f.addEventListener('submit', checkAgree, false)});
}, false);
</script>

<form name="chbox">
<label><input type=checkbox name="cBox"> 上記、「システム利用規約」に同意します。</label>
</form>

<a href="page1.html" class=agreed>リンクで遷移する場合</a>
<a href="page2.html" class=agreed>複数でも大丈夫</a>

<form method=GET action="page3.html" class=agreed>
<button type=submit>サブミットで遷移する場合</button>
</form>
<form method=GET action="page4.html" class=agreed>
<button type=submit>複数でも大丈夫</button>
</form>
    • good
    • 0
この回答へのお礼

おおお!すごい!天才ですか!?
できました!本当にありがとうございました!!
感謝感謝です!

yambejpもありがとうございました!!

お礼日時:2014/08/11 22:52

アンカータグでやるならむしろurlをユーザー関数に渡す必要もないのでは?


またformをnameで参照する方法はちょっと古くてあまりお勧めできません。
cBoxをずばりidで指定した方がよいような気がします。

<html>
<head>
<script>
function goServlet(url){
if (!document.getElementById("cBox").checked){
window.alert("同意して");
return false;
}else{
return true;
}
}
</script>
</head>
<body>
<p>上記、「システム利用規約」に同意します。
<form name="chbox">
<input type="checkbox" name="cBox" id="cBox">
</form>
</p>
<a href="http://google.com" onclick="return goServlet()" >トップページへはこちら</a>
<a href="http://yahoo.jp" onclick="return goServlet()" >会員ページへはこちら</a>
</body>
</html>
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!