简体   繁体   English

简单的Javascript OnClick功能无法正常工作

[英]Simple Javascript OnClick function not working

I am trying to create a simple captcha using javascript by creating a math question. 我试图通过创建一个数学问题使用JavaScript创建一个简单的验证码。 This is not working. 这不起作用。 Any idea on what I am doing wrong? 对我做错了什么的想法?

What does four + 3 equal? <br>
<input type="text" name="secquestion" id="secquestion" value="" placeholder="Type Numberic Answer Here (Ex. 9)">


 <input type="submit" value="  Send  " class="button" name="button" onclick="if (value !== "7") {alert('You must answer the security question correctly!');return false}">

the problem is the double quotes used in your conditional statement are messing with the double quotes around the onclick values. 问题是你的条件语句中使用的双引号是乱搞onclick值周围的双引号。 use this 用这个

<input type="submit" value=" Send " class="button" name="button" onclick="if (value !== '7') {alert('You must answer the security question correctly!');return false}">

You needed to remove the double quotes and additionally you need to tell your conditional function which elements value you are interested in. 你需要删除双引号,另外你需要告诉你的条件函数你感兴趣的元素值。

By using javascripts documentGetElementById we can specify which element on the document we are interested in. Then we select the value attribute in order to grab the input from the user and test it in our conditional statement. 通过使用javascripts documentGetElementById我们可以指定我们感兴趣的文档中的哪个元素。然后我们选择value属性以获取用户的输入并在条件语句中测试它。

    What does four + 3 equal? <br>
    <input type="text" name="secquestion" id="secquestion" value="" placeholder="Type Numberic Answer Here (Ex. 9)">


     <input type="submit" value="  Send  " class="button" name="button" onclick="if(document.getElementById('secquestion').value != 7) {alert('You must answer the security question correctly!');return false}">

Personally, it might be easier to maintain this code by breaking this conditional out into its own function like so: 就个人而言,通过将此条件限制为其自己的函数来维护此代码可能更容易:

    <html>
    What does four + 3 equal? <br>
    <input type="text" name="secquestion" id="secquestion" value="" placeholder="Type Numberic Answer Here (Ex. 9)">


    <input type="submit" value="  Send  " class="button" name="button" onclick="checkCaptcha()">

    <script type="text/javascript">
        function checkCaptcha(){
           if(document.getElementById("secquestion").value != 7){

               alert('You must answer the security question correctly!');

               return false;
           }

           return true;
         }
    </script>
    </html>

The issue is only with doublequotes. 这个问题只有双引号。

The below code works. 以下代码有效。

What does four + 3 equal? <br>
<input type="text" name="secquestion" id="secquestion" value="" placeholder="Type Numberic Answer Here (Ex. 9)">


 <input type="submit" value="  Send  " class="button" name="button" onclick="if (value !== '7') {alert('You must answer the security question correctly!');return false}">

JSFiddle 的jsfiddle

Your quotes are off. 你的报价已经关闭。

onclick="if (value !== "7")....

needs to be onclick="if (value !== '7').... 需要onclick =“if(value!=='7')....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM