フォームに onsubmit = "return false;" と記述すると、フォームの送信を妨げることができますが、この方法では送信を妨げることはできません。違法なコンテンツによるフォームの送信 例: このコードに多くの関数を記述しましたが、フォームの送信を妨げるのが違法な場合、どうすればそれを阻止できますか? 「return false」という文を1つずつ追加しますか?
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title> </title> <style type="text/css"> div input{display: inline-block;width: 200px;height: 20px;border:1px solid #ccc;} div .span1{display: inline-block;width: 90px;height: 20px;text-align: left;margin-top: 20px;} div #sp1,#sp2,#sp3,#sp4{display: inline-block;width: 160px;height: 20px;} </style> <script src="ajax.js"></script> <script type="text/javascript"> window.onload=function(){ var oInput=document.getElementsByTagName('input'); var oIpt1=document.getElementById('ipt1'); var oIpt2=document.getElementById('ipt2'); var oIpt3=document.getElementById('ipt3'); var oIpt4=document.getElementById('ipt4'); var oSp1=document.getElementById('sp1'); var oSp2=document.getElementById('sp2'); var oSp3=document.getElementById('sp3'); var oSp4=document.getElementById('sp4'); oIpt1.value = ''; oIpt2.value = ''; $("input[type=reset]").trigger("click"); var re=/^[\w]{6,12}$/; function check(){ return false; }; oIpt1.onblur=function(){ if(oIpt1.value==''){ this.style.border='1px solid red'; }else{ ajax("nameajax.php?id="+oIpt1.value,function(str){ oSp1.innerHTML=str; }) } }; oIpt2.onblur=function(){ if(oIpt2.value==''){ this.style.border='1px solid red'; }else{ if(oIpt2.value.match(re)){ oSp2.innerHTML='密码合法'; }else{ oSp2.innerHTML='密码不合法'; } } } oIpt3.onblur=function(){ if(oIpt3.value==''){ this.style.border='1px solid red'; }else{ if(oIpt2.value==oIpt3.value){ oSp3.innerHTML='密码正确'; }else{ oSp3.innerHTML='密码不一致'; } } }; oIpt4.onblur=function(){ if(oIpt4.value==''){ this.style.border='1px solid red'; }else{ ajax("emailajax.php?id="+oIpt4.value,function(str){ oSp4.innerHTML=str; }) } }; }; </script></head><body><div> <form action="reg.php" onsubmit="return check();" method="post"> <span class="span1">用 户 名:</span><input type="text" id="ipt1" name="username" autocomplete="off"><span id="sp1"></span><br /> <span class="span1">密 码:</span><input type="password" id="ipt2" autocomplete="off"><span id="sp2"></span><br /> <span class="span1">确认密码:</span><input type="password" id="ipt3" name="password" autocomplete="off"><span id="sp3"></span><br /> <span class="span1">邮 箱:</span><input type="text" id="ipt4" name="email" autocomplete="off"><span id="sp4"></span><br /> <button type="submit">提交</button> </form></div></body></html>
onsubmit="return check();" であるため、check() には戻り値があるはずです。
チェック関数の書き方は?衝突を共有することを心配していませんか?
チェック関数の書き方は?衝突を共有することを心配していませんか?
false を直接返すことはできませんか?
onsubmit="return check();" なので、check() には戻り値があるはずです。
;
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title> </title> <style type="text/css"> div input{display: inline-block;width: 200px;height: 20px;border:1px solid #ccc;} div .span1{display: inline-block;width: 90px;height: 20px;text-align: left;margin-top: 20px;} div #sp1,#sp2,#sp3,#sp4{display: inline-block;width: 160px;height: 20px;} </style> <script src="ajax.js"></script> <script type="text/javascript"> window.onload=function(){ var oInput=document.getElementsByTagName('input'); var oIpt1=document.getElementById('ipt1'); var oIpt2=document.getElementById('ipt2'); var oIpt3=document.getElementById('ipt3'); var oIpt4=document.getElementById('ipt4'); var oSp1=document.getElementById('sp1'); var oSp2=document.getElementById('sp2'); var oSp3=document.getElementById('sp3'); var oSp4=document.getElementById('sp4'); oIpt1.value = ''; oIpt2.value = ''; $("input[type=reset]").trigger("click"); var re=/^[\w]{6,12}$/; function check(){ //if(条件){ // document.getElementById('form1').submit(); //} return false; }; oIpt1.onblur=function(){ if(oIpt1.value==''){ this.style.border='1px solid red'; }else{ ajax("nameajax.php?id="+oIpt1.value,function(str){ oSp1.innerHTML=str; }) } }; oIpt2.onblur=function(){ if(oIpt2.value==''){ this.style.border='1px solid red'; }else{ if(oIpt2.value.match(re)){ oSp2.innerHTML='密码合法'; }else{ oSp2.innerHTML='密码不合法'; } } } oIpt3.onblur=function(){ if(oIpt3.value==''){ this.style.border='1px solid red'; }else{ if(oIpt2.value==oIpt3.value){ oSp3.innerHTML='密码正确'; }else{ oSp3.innerHTML='密码不一致'; } } }; oIpt4.onblur=function(){ if(oIpt4.value==''){ this.style.border='1px solid red'; }else{ ajax("emailajax.php?id="+oIpt4.value,function(str){ oSp4.innerHTML=str; }) } }; }; </script></head><body><div> <form action="reg.php" id="form1" onsubmit="return false" method="post"> <span class="span1">用 户 名:</span><input type="text" id="ipt1" name="username" autocomplete="off"><span id="sp1"></span><br /> <span class="span1">密 码:</span><input type="password" id="ipt2" autocomplete="off"><span id="sp2"></span><br /> <span class="span1">确认密码:</span><input type="password" id="ipt3" name="password" autocomplete="off"><span id="sp3"></span><br /> <span class="span1">邮 箱:</span><input type="text" id="ipt4" name="email" autocomplete="off"><span id="sp4"></span><br /> <button type="button" onclick="check()">提交</button> </form></div></body></html>
変更されました、いいえ、フォームが変更されましたも変更されました
function check(){<!DOCTYPE html><html><head> <meta charset="utf-8"> <title> </title> <style type="text/css"> div input{display: inline-block;width: 200px;height: 20px;border:1px solid #ccc;} div .span1{display: inline-block;width: 90px;height: 20px;text-align: left;margin-top: 20px;} div #sp1,#sp2,#sp3,#sp4{display: inline-block;width: 160px;height: 20px;} </style> <script src="ajax.js"></script> <script type="text/javascript"> window.onload=function(){ var oInput=document.getElementsByTagName('input'); var oIpt1=document.getElementById('ipt1'); var oIpt2=document.getElementById('ipt2'); var oIpt3=document.getElementById('ipt3'); var oIpt4=document.getElementById('ipt4'); var oSp1=document.getElementById('sp1'); var oSp2=document.getElementById('sp2'); var oSp3=document.getElementById('sp3'); var oSp4=document.getElementById('sp4'); oIpt1.value = ''; oIpt2.value = ''; $("input[type=reset]").trigger("click"); var re=/^[\w]{6,12}$/; function check(){ //if(条件){ // document.getElementById('form1').submit(); //} return false; }; oIpt1.onblur=function(){ if(oIpt1.value==''){ this.style.border='1px solid red'; }else{ ajax("nameajax.php?id="+oIpt1.value,function(str){ oSp1.innerHTML=str; }) } }; oIpt2.onblur=function(){ if(oIpt2.value==''){ this.style.border='1px solid red'; }else{ if(oIpt2.value.match(re)){ oSp2.innerHTML='密码合法'; }else{ oSp2.innerHTML='密码不合法'; } } } oIpt3.onblur=function(){ if(oIpt3.value==''){ this.style.border='1px solid red'; }else{ if(oIpt2.value==oIpt3.value){ oSp3.innerHTML='密码正确'; }else{ oSp3.innerHTML='密码不一致'; } } }; oIpt4.onblur=function(){ if(oIpt4.value==''){ this.style.border='1px solid red'; }else{ ajax("emailajax.php?id="+oIpt4.value,function(str){ oSp4.innerHTML=str; }) } }; }; </script></head><body><div> <form action="reg.php" id="form1" onsubmit="return false" method="post"> <span class="span1">用 户 名:</span><input type="text" id="ipt1" name="username" autocomplete="off"><span id="sp1"></span><br /> <span class="span1">密 码:</span><input type="password" id="ipt2" autocomplete="off"><span id="sp2"></span><br /> <span class="span1">确认密码:</span><input type="password" id="ipt3" name="password" autocomplete="off"><span id="sp3"></span><br /> <span class="span1">邮 箱:</span><input type="text" id="ipt4" name="email" autocomplete="off"><span id="sp4"></span><br /> <button type="button" onclick="check()">提交</button> </form></div></body></html>
$はjqueryを導入していないからです