Javascript 按钮onclick不调用函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29002220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:44:32  来源:igfitidea点击:

Button onclick not calling function

javascripthtmlbuttononclick

提问by user3642768

My button code looks like this:

我的按钮代码如下所示:

<button onclick="submitFunction()">Submit</button>

And my function code like this:

我的函数代码是这样的:

function submitFunction() {
    alert("example");
}

I've been looking online for an answer and tried a bunch of things, but nothing seems to work. I'm just putting the alert there to see if the function is being called at all. I'm using the following to link the files.

我一直在网上寻找答案并尝试了很多东西,但似乎没有任何效果。我只是将警报放在那里,看看是否正在调用该函数。我正在使用以下链接文件。

<script src="file.js"></script>

回答by hamed

Probably browser can not find your external js file, because of wrong path. Try to put function inside the <head>section of the page and if worked, The problem is wrong path to js file.

由于路径错误,浏览器可能找不到您的外部 js 文件。尝试将函数放在<head>页面的部分中,如果有效,问题是 js 文件的路径错误。

回答by Sadikhasan

You have to write following javascriptcode between <head>tag and make sure your given javascript file have valid path.

您必须javascript<head>标记之间编写以下代码,并确保给定的 javascript 文件具有valid path.

JavaScript

JavaScript

<script type="text/javascript">
  function submitFunction() {
      alert("example");
  }
</script>

Demo

演示

回答by Pranali Maske

Instead you can call jquery and call function externally in script tag.

相反,您可以在脚本标记中从外部调用 jquery 和调用函数。

Ex:

前任:

$("button").on("click",function(){alert("hi")})

回答by Binoy Kumar

Give your function at the very beginning of your external js file .

在外部 js 文件的开头给出你的函数。

回答by Brent Jefferson

In case it helps anyone in the future, when having link problems like this I've found it useful to:

以防将来它对任何人有帮助,当遇到这样的链接问题时,我发现它对以下方面很有用:

  1. Add a HTML link to the file <a href="file.js">file.js</a>and click it in the browser to see where it takes you.
  2. Remind it to look in the current directory (provided it is definitely in the current directory) with "./" "./file.js"(For some strange reason it seems to sometimes help—or I'm crazy—which is more likely.)
  1. 向该文件添加一个 HTML 链接<a href="file.js">file.js</a>,然后在浏览器中单击它以查看它会将您带到何处。
  2. 提醒它使用“./”查看当前目录(前提是它肯定在当前目录中)"./file.js"(出于某种奇怪的原因,它有时似乎有帮助——或者我疯了——这更有可能。)

回答by wilson wilson

If your function happens to be in the global scope, try adding window.before the function. Like this:

如果您的函数恰好在全局范围内,请尝试window.在函数之前添加。像这样:

<button onclick="window.submitFunction()">Submit</button>

<button onclick="window.submitFunction()">Submit</button>

function submitFunction() {
    alert("example");
}

This worked for me.

这对我有用。

I have seen other people specify the button type and then add return false after the function like this:

我见过其他人指定按钮类型,然后在函数后添加 return false ,如下所示:

<button type="button" onclick="window.submitFunction(); return false;">Submit</button>This didn't change anything for me, but It may work in your case. You could give it a try.

<button type="button" onclick="window.submitFunction(); return false;">Submit</button>这对我没有任何改变,但它可能适用于您的情况。你可以试一试。

回答by Richa Chauhan

I also faced the same issue, then realised the I was not giving the proper type attribute in the script tag, instead of giving tag as:

我也遇到了同样的问题,然后意识到我没有在脚本标签中提供正确的类型属性,而不是将标签提供为:

type="text/javascript"

I was giving it as:

我给它作为:

type="text/script"