質問

javascriptがfalseを返した場合でも、Plsチェックコード.htmlフォームが送信されます。

<form id="form1" name="form1"   method="post" action="sub.jsp" onsubmit="return getValue()">
<input type="text" id="userName" name="userName"   onkeyup="return getValue()" />
<input type="submit" name="Submit" value="Submit" />
</form>

    <script type="text/javascript" >
    function getValue()
      {
        var userName=document.getElementById("userName");
            document.getElementById("userNamemsg").innerHTML="";
              if(userName.value=="")
              {
             var mdiv=document.getElementById("userNamemsg");
                  mdiv.innerHTML="Error:Required Fields cannot be blank!";
                  form.userName.focus();
                  return false;
               }
              return true;   
     } 
役に立ちましたか?

解決

1)行 form.userName.focus(); document.form1.userName.focus();

または

2)関数自体から送信してみてください:

<input type="button" name="Submit" value="Submit" onclick="getValue()" />

<script type="text/javascript" >
function getValue()
  {
        var userName=document.getElementById("userName");
        document.getElementById("userNamemsg").innerHTML="";
          if(userName.value=="")
          {
              var mdiv=document.getElementById("userNamemsg");
              mdiv.innerHTML="Error:Required Fields cannot be blank!";
              document.form1.userName.focus();//I think this is the problem
              return false;
           }
          document.form1.submit();
 }
 </script>

他のヒント

JavaScriptコードに、returnステートメントの前に発生するエラーがあると思います。これらのエラーを修正すると、コードが機能するはずです。

あるいは、送信ボタンのクリックハンドラーをfalseに戻すようにします。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top