简体   繁体   English

如何从onClick调用函数

[英]How to call a function from onClick

I'm trying to make a countdown timer that starts when you click the button. 我正在尝试创建一个倒计时计时器,当您单击按钮时该计时器开始计时。 I can't seem to get the function to be called with the onclick. 我似乎无法使用onclick调用该函数。 What is the proper syntax for calling a function with string parameters from onclick? 从onclick调用带有字符串参数的函数的正确语法是什么? I tried: 我试过了:

onclick="countDown(10, 'status')" 

but that doesn't work. 但这不起作用。 Anyone know what I'm doing wrong? 有人知道我在做什么错吗?

Demo: 演示:

 function countDown(secs,elem) { var element = document.getElementById(elem); element.innerHTML = "Please wait for "+secs+" seconds"; if(secs<1){ clearTimeout(timer); element.innerHTML = '<h2>Countdown Complete!</h2>'; element.innerHTML += '<a href="#"> Click here now</a>'; } secs--; var timer = setTimeout('countDown('+secs+',"'+elem+'")',1000); } 
 <div id="status"></div> <form> <input type="submit" name="go" onclick="countDown(5, 'status'); return false;"> </form> 

http://codepen.io/michaelaharvey/pen/WrGxYp http://codepen.io/michaelaharvey/pen/WrGxYp

That is the correct syntax (at least if you are going to use intrinsic event attributes), however you have used it on a submit button so a split second after the function runs, the form submits and it loads a new page (which wipes out the changes you made with JS). 这是正确的语法(至少在要使用内部事件属性的情况下),但是在提交按钮上使用了它,因此在函数运行后的一瞬间,表单将提交并加载新页面(这将清除掉该页面)您使用JS所做的更改)。

You can return false from the onclick function to prevent the default behaviour: 您可以从onclick函数返回false,以防止出现默认行为:

onclick="countDown(10, 'status'); return false;" 

Modern code would lean towards using JavaScript to bind event handlers. 现代代码倾向于使用JavaScript绑定事件处理程序。

document
    .querySelector("input[type=submit]")
    .addEventListener("click", function (event) {
        event.preventDefault();
        countDown(10, 'status');
});

您可以通过单击按钮来调用javascript函数,如下所示:

    <input type="submit" name="go" onclick="countDown(10, 'status'); return false">

The problem is that you're using a submit type of button. 问题是您使用的是提交类型的按钮。 Return false if you need to avoid submitting the form: 如果您需要避免提交表单,则返回false

<div id="status"></div>

<form>
<input type="submit" name="go" onclick="countDown(10, 'status'); return false;">
</form>

There are a couple of things, look for the changes here: http://codepen.io/anon/pen/EPggNJ 有几件事,请在此处查找更改: http : //codepen.io/anon/pen/EPggNJ

Summary: 摘要:

  • you have a local variable secs which is always decremented from the starting level 10, so it's always 9 and never gets less than that 您有一个局部变量secs ,它总是从起始级别10开始递减,因此它始终为9并且永远不会小于该值
  • The submit control type immediately sends the form and reloads the page 提交控件类型立即发送表单并重新加载页面
  • setting a timer even when the value of secs is less than 0 due to missing return 设置计时器,即使由于丢失返回而使secs值小于0时

The above linked fork works as expected. 上面链接的fork可以正常工作。

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

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