首页 > 解决方案 > 脚本中没有“弹出”窗口的JavaScript?

问题描述

我希望发送按钮(id="Button1")仅在标题正确(string1 === string2)时发送邮件。在按钮本身还是在脚本中,我是如何以及如何让它工作的?

我也不希望弹出窗口显示告诉我“真”,与假一样,我确实希望“警报”打开并告诉我什么时候出错,但在告诉我“假”之后不要签名。我试过没有“return false;” 和“返回真;”,但这只会给我“未定义”。 编辑:刚刚解决了这个问题,我没有意识到我有“alert(ValidCaptcha());” 它应该是“ValidCaptcha();”。但仍然需要第一个问题的帮助。

function Captcha(){
         var alpha = new Array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','Å','Ä','Ö',
            'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','å','ä','ö', 
                '0','1','2','3','4','5','6','7','8','9');
         var i;
         for (i=0;i<6;i++){
             var a = alpha[Math.floor(Math.random() * alpha.length)];
             var b = alpha[Math.floor(Math.random() * alpha.length)];
             var c = alpha[Math.floor(Math.random() * alpha.length)];
             var d = alpha[Math.floor(Math.random() * alpha.length)];
             var e = alpha[Math.floor(Math.random() * alpha.length)];
             var f = alpha[Math.floor(Math.random() * alpha.length)];
             var g = alpha[Math.floor(Math.random() * alpha.length)];
                          }
             var code = a + ' ' + b + ' ' + ' ' + c + ' ' + d + ' ' + e + ' '+ f + ' ' + g;
             document.getElementById("mainCaptcha").innerHTML = code
             document.getElementById("mainCaptcha").value = code
           }
    function ValidCaptcha(){
         var string1 = removeSpaces(document.getElementById('mainCaptcha').value);
         var string2 = removeSpaces(document.getElementById('txtInput').value);
         
      if (string1 === string2){
                return true;
         }
      else
      {        
         alert('Wrong, try again');
              return false;
              }
    }
    function removeSpaces(string){
         return string.split(' ').join('');
    }
.capt{
        background-color:LightGray;
        width: 300px;
        height:100px;
        
    }

    #mainCaptcha{
        position: relative;
        left : 60px;
        top: 5px;
        
    }

    #refresh{
        position:relative;
        left:230px;
        width:30px;
        height:30px;
        bottom:45px;
    }

    #txtInput, #Button1{
        position: relative;
        left:20px;
        bottom: 40px;
    }
<body onload="Captcha();"> 
       <div class="capt"> 
       <h2 style="-moz-user-select: none; -webkit-user-select: none; -ms-user-select:none; user-select:none;-o-user-select:none;" unselectable="on" onselectstart="return false;" onmousedown="return false;" type="text" id="mainCaptcha"></h2>
       <p><input type="button" id="refresh" onclick="Captcha();"/></p>            <input type="text" id="txtInput"/>    
       
       <button value="Send" id="Button1" type="submit" onclick="alert(ValidCaptcha());">SEND</button>
       </div>
    </body>

标签: javascripthtml

解决方案


您正在警报中传递 ValidCaptcha,这就是为什么它返回警报与您的 return True 和 false 我删除了警报调用并且它工作正常

function Captcha(){
         var alpha = new Array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','Å','Ä','Ö',
            'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','å','ä','ö', 
                '0','1','2','3','4','5','6','7','8','9');
         var i;
         for (i=0;i<6;i++){
             var a = alpha[Math.floor(Math.random() * alpha.length)];
             var b = alpha[Math.floor(Math.random() * alpha.length)];
             var c = alpha[Math.floor(Math.random() * alpha.length)];
             var d = alpha[Math.floor(Math.random() * alpha.length)];
             var e = alpha[Math.floor(Math.random() * alpha.length)];
             var f = alpha[Math.floor(Math.random() * alpha.length)];
             var g = alpha[Math.floor(Math.random() * alpha.length)];
                          }
             var code = a + ' ' + b + ' ' + ' ' + c + ' ' + d + ' ' + e + ' '+ f + ' ' + g;
             document.getElementById("mainCaptcha").innerHTML = code
             document.getElementById("mainCaptcha").value = code
           }
    function ValidCaptcha(){
         var string1 = removeSpaces(document.getElementById('mainCaptcha').value);
         var string2 = removeSpaces(document.getElementById('txtInput').value);
         
      if (string1 === string2){
                return true;
         }
      else
      {        
         alert('Wrong, try again');
              return false;
              }
    }
    function removeSpaces(string){
         return string.split(' ').join('');
    }
.capt{
        background-color:LightGray;
        width: 300px;
        height:100px;
        
    }

    #mainCaptcha{
        position: relative;
        left : 60px;
        top: 5px;
        
    }

    #refresh{
        position:relative;
        left:230px;
        width:30px;
        height:30px;
        bottom:45px;
    }

    #txtInput, #Button1{
        position: relative;
        left:20px;
        bottom: 40px;
    }
<body onload="Captcha();"> 
       <div class="capt"> 
       <h2 style="-moz-user-select: none; -webkit-user-select: none; -ms-user-select:none; user-select:none;-o-user-select:none;" unselectable="on" onselectstart="return false;" onmousedown="return false;" type="text" id="mainCaptcha"></h2>
       <p><input type="button" id="refresh" onclick="Captcha();"/></p>            <input type="text" id="txtInput"/>    
       
       <button value="Send" id="Button1" type="submit" onclick="ValidCaptcha();">SEND</button>
       </div>
    </body>


推荐阅读