[英]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.