简体   繁体   English

按钮未使用JavaScript链接到输入字段

[英]Button not linking to input field with javascript

I want the button to run the code in the corresponding function field, but I don't believe I have it set up correctly. 我希望按钮在相应的函数字段中运行代码,但我不认为我已正确设置了它。 The string or numbers entered in the field should be processed and then the answer should be printed out. 应该处理在字段中输入的字符串或数字,然后将答案打印出来。 The last 'else' statement is simply to see if it runs the code, which it did, but I could enter both numbers and strings and the first statement would be printed "This is not a year". 最后一个'else'语句只是看它是否运行了代码,确实如此,但是我可以同时输入数字和字符串,第一个语句将显示为“这不是一年”。 I also am not sure that I formatted the second 'else/if' statements parameters of (enter % 4 && enter % 100 && !enter % 400) correctly. 我也不确定我是否正确格式化了第二个'else / if'语句参数(输入%4 &&输入%100 &&!enter%400)。 Any help would be appreciated as I have tried to resolve this on my own and have progressed slowly, so any help is much appreciated. 任何帮助将不胜感激,因为我尝试自己解决该问题并且进展缓慢,因此非常感谢任何帮助。 Thank you! 谢谢!

    <!DOCTYPE html>
    <html>
    <head>
    <link rel="stylesheet" href="style.css" />
    <script src='script.js'></script>
    <title></title>
    </head>
    <body>
    <!--leap year calculator-->
    <h3>Is it a leap year?</h3>

    <form action="">
        Year:<input type="text" name="enter" id="enter"> <br>
    </form>
    <script>function YOLO(enter){
    if (iNaN(enter)){
    console.log("This is not a year");
    }
    else if (enter % 4 && enter % 100 && !enter % 400){console.log("no");
    }
    else if(enter % 4){console.log("yes");
    }
    else {alert("working")}
    };

    var Id= document.getElementById("enter");
    </script>
    <button onclick="YOLO(Id)">Enter</button>

    </body>
    </html>

You must work with value property of element. 您必须使用element的value属性。 Something like this: 像这样:

<button onclick="YOLO(document.getElementById('enter').value);">Calculate</button>

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

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