简体   繁体   English

按下按钮时如何执行if函数(javascript)

[英]how to perform an if function when a button is pressed (javascript)

HTML HTML

<button id="submit" onClick="">Finish questionnaire</button><br/>

Javascript Javascript

var submitButton = document.getElementById('submit');
var x = 0;

$(":radio").change(function() {

    x = x + 1

    if (x == 3) {
        alert("Submitted")
    } else {
        alert("not submitted")
    }

});

I am making a short questionnaire website.我正在制作一个简短的问卷网站。 I would like to have submit button at the bottom of my page which will run the javascript code.我想在我的页面底部有一个提交按钮,它将运行 javascript 代码。 This code will either give an alert of submit or not submitted.此代码将发出提交或未提交的警报。 If the user has answered all the questions, then the alert will say submitted, otherwise, if a question is missed out, it will say not submitted.如果用户已经回答了所有问题,那么警报会说已提交,否则,如果错过了一个问题,它会说未提交。 I have my javascript code, but I dont know how to implement it with a button.我有我的 javascript 代码,但我不知道如何用按钮实现它。 Please help请帮忙

$("#submit").click( function() {
   // whatever you want
});

只需将其包装在.click(function(){CODE HERE})

You need eventListener for button click.您需要 eventListener 来单击按钮。 A very simple Google search can give you the answer fast enough.一个非常简单的谷歌搜索可以足够快地给你答案。 Still for your convenience, I'm writing the answer here for button click.仍然为了您的方便,我在这里写了按钮点击的答案。

var submitButton = document.getElementById('submit');
var x = 0;

submitButton.addEventListener('click', function() {

    x = x + 1

    if (x == 3) {
        alert("Submitted")
    } else {
        alert("not submitted")
    }

});

Try this;尝试这个;

 var x = 0; $('#submit').click(function() { x = x + 1 if (x == 3) { alert("Submitted") } else { alert("not submitted") } });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="submit">Finish questionnaire</button><br/>

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

相关问题 当用jQuery Mobile按下后退按钮时,如何执行两个动作? - How to perform two actions when the back button is pressed with jQuery Mobile? 按下按钮时如何使JavaScript工作? - How to make JavaScript work when button is pressed? Javascript 反应:按下按钮时按钮不执行 function - Javascript React: Button not excuting function when button is pressed 当按下“完成”按钮时,datetimepicker触发javascript函数 - datetimepicker trigger a javascript function when the 'Done' button is pressed 通过Javascript按下输入按钮时调用一个函数 - Call a function when the enter button is pressed via Javascript Google扩展:如何在弹出窗口中按下按钮时在我的javascript文件中运行功能? - Google extensions: How to run a function in my javascript file when button is pressed in the popup? 在 HTML 代码中按下按钮时,如何触发 JavaScript function - How do I trigger a JavaScript function when a button is pressed in HTML code 如果其他情况下按下按钮时 - If Else for when a button is pressed javascript JavaScript-计算按下按钮的次数(功能) - JavaScript - Count how many time a button is pressed(function) 使用javascript按下按钮时如何打开一个窗口并关闭另一个窗口? - How to open a window and close another window when a button is pressed with javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM