如何在 PHP 中调用外部 javascript 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5864228/
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 19:14:38  来源:igfitidea点击:

How to call external javascript file in PHP?

phpjavascriptexternal

提问by Newbie Coder

I am learning how to call external javascript files in my PHP code. I got some codes from the internet and tried it but its not working. Can somebody pls give me some advice or explain to me this. I am not a programmer but I am studying how to program and just started learning that's why I have difficulty understanding some concepts.

我正在学习如何在我的 PHP 代码中调用外部 javascript 文件。我从互联网上得到了一些代码并尝试了它,但它不起作用。有人可以给我一些建议或向我解释这一点。我不是程序员,但我正在学习如何编程并且刚刚开始学习这就是为什么我很难理解一些概念。

I have here the codes, PHP File and JS file. They are in the same folder.

我这里有代码、PHP 文件和 JS 文件。它们在同一个文件夹中。

Here are the codes:

以下是代码:

index.php

索引.php

<html>
<head>
 <script language="JavaScript" src="exer_1.js"></script>
</head>
<body>
 <form name="myform">
  <input type="text" id="input_1" name="input_1" /><br />
  <input type="text" id="input_2" name="input_2" /><br />
  <input type="submit" value="Check!" onclick="javascript:parseTest() return false;" />
 </form>
</body>
</html>

exer_1.js

exer_1.js

function parseTest() {
 var elem_1 = document.getElementById('input_1');
 var elem_2 = document.getElementById('input_2');

 var inp_1 = elem_1.value;
 var inp_2 = elem_2.value;

 if (inp_1 == "" && inp_2 == "") {
  alert("You need to enter integers!!!");
  elem_1.focus();
 } else if (inp_1 == ""){
  alert("You need to enter Integer 1!!!");
  elem_1.focus();
 } else if (inp_2 == ""){
  alert("You need to enter Integer 2!!!");
  elem_2.focus();;
 } else {
  if (!parseInt(inp_1) || !parseInt(inp_2)) alert ("Enter Integers only!!!");
  else {
   alert("Correct Inputs!!!");
  }
 } 
}

回答by Phil

<script language="JavaScript" src="exer_1.js"></script>

<script language="JavaScript" src="exer_1.js"></script>

The languageattribute is deprecated, use typeinstead

language属性已弃用,请type改用

<script type="text/javascript" src="exer_1.js"></script>

The correct syntax for inline event binding is

内联事件绑定的正确语法是

<input type="submit" value="Check!" onclick="parseTest(); return false;" />


You may want to consider moving the event handler to the form's submit event. Your function could then return falseon error or trueon success, which can then be used to determine whether the form submission continues or not, eg

您可能需要考虑将事件处理程序移动到表单的提交事件。然后您的函数可以false在错误或true成功时返回,然后可用于确定表单提交是否继续,例如

<form onsubmit="return parseTest()">

回答by Zero

<script type="text/javascript" src="exer_1.js"></script>

回答by Ibu

You have not really specified what is not working but i am noticing something in your code.

你还没有真正指定什么不起作用,但我注意到你的代码中有一些东西。

<html>
<head>
 <script language="JavaScript" src="exer_1.js"></script>
</head>
<body>
 <form name="myform">
  <input type="text" id="input_1" name="input_1" /><br />
  <input type="text" id="input_2" name="input_2" /><br />
 <!-- The following will cause an error -->
  <input type="submit" value="Check!" onclick="javascript:parseTest() return false;" />

<!-- instead use this -->
<input type="submit" value="Check!" onclick="javascript:parseTest(); return false;" />
 </form>
</body>
</html>

semicolon error

分号错误

回答by Dietpixel

I would look into JQuery for a nice javascript framework. Instead of putting javascript code on the button's "onclick" event, with jquery you could do something like:

我会研究 JQuery 以获得一个不错的 javascript 框架。不是将 javascript 代码放在按钮的“onclick”事件上,而是使用 jquery,您可以执行以下操作:

$('#submit').click(function() {
  // whatever code you want to run when submit is clicked.
  alert('Submit clicked');
}