简体   繁体   English

为什么当我点击复选框时没有调用函数onclick?

[英]Why when I click on the checkbox doesn't call the function onclick?

 function click() { alert("function is called"); } 
 <form action="regCheck.aspx" method="post" onsubmit="return submitF()"> <input type="checkbox" name="checkboxs" onclick="click()"/> </form> 

This is not my real file. 这不是我的真实档案。 I'm just writing this part of my code so you people could see why my onclick function is not being called. 我只是编写我的代码的这一部分,以便人们可以看到为什么我的onclick函数没有被调用。 If the javascript opens a window of alert when I click the checkbox, then it means I can use it. 如果单击复选框时javascript打开一个警告窗口,则表示我可以使用它。 I hope you can help me. 我希望你能帮助我。 Thanks! 谢谢!

Just change the name of your function from "click" to another name, so that it's not shadowed by a the native click function of the input ): 只需将您的函数名称从"click"更改为另一个名称,这样它就不会被输入本机click功能遮蔽:

 function notcalledclick() { alert("function is called"); } 
 <form action="regCheck.aspx" method="post" onsubmit="return submitF()"> <input type="checkbox" name="checkboxs" onclick="notcalledclick()"/> </form> 

This is another reason to avoid adding inline javascript and prefer addEventListener : 这是避免添加内联javascript并且更喜欢addEventListener另一个原因:

 document.getElementById('someId').addEventListener('click', function(){ alert("function is called"); }); 
 <form action="regCheck.aspx" method="post" onsubmit="return submitF()"> <input id=someId type="checkbox" name="checkboxs"/> </form> 

Your function name should change. 您的函数名称应该更改。

There's already a function called click, responsible for calling the event handler. 已经有一个名为click的函数,负责调用事件处理程序。 By declaring another, you override the first, so the event doesn't work anymore. 通过声明另一个,您覆盖第一个,因此事件不再起作用。

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

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