簡體   English   中英

表單提交按鈕onclick不會調用我的JS文件中包含的JS函數

[英]Form submit button onclick does not call my JS function included in JS file

我有一個帶有提交按鈕的html表單。 我在頭中包含的文件中有一個js函數。 我試圖綁定按鈕的onclick事件以調用該函數。 但是,我嘗試的所有方法均無效。

<!DOCTYPE html>
<html>
<body>
    <script src="popup.js" type="text/javascript"></script>
    <button type="button" onclick="displayFormContents();">
</button>

<p id="demo"></p>
First name:<br>
<input type="text" name="firstname" >
<br>
Name:<br>
<input type="text" name="name" >
<br>

<form>
    age : <input type="date" name="Age">
</form>

<form>
    Phone : <input type="number" name="Phone">
</form>

<form>
    email : <input type="email" name="mail">
</form>

<form>
    Student at UCLA? : <input type="checkbox" name="Doe">
</form>

<form>
    <input type="radio" name="gender" value="male" checked> Male<br>
    <input type="radio" name="gender" value="female"> Female<br>
    <input type="radio" name="gender" value="other"> Other
</form>

<input type="submit">
</form>

</body>
</html>

確保在popup.js文件中創建了displayFormContents()方法,然后無需多次使用form標簽,將所有form元素都放在一個form標簽中。

<html>
<body>
<script src="popup.js" type="text/javascript"></script>
<form>
<p id="demo"></p>
 First name:<br>
  <input type="text" name="firstname" >
  <br>
  Name:<br>
  <input type="text" name="name" >
  <br>
  age :
  <input type="date" name="Age">
  Phone :
  <input type="number" name="Phone">
  email :
  <input type="email" name="mail">
  Student at UCLA? :

  <input type="checkbox" name="Doe">
    <input type="radio" name="gender" value="male" checked> Male<br>

  <input type="radio" name="gender" value="female"> Female<br>

  <input type="radio" name="gender" value="other"> Other

<button type="button"
onClick="displayFormContents();">Submit
</button>
</form>
</body>

</html>

僅使用一個表單標簽。 popup.js應與包含此代碼的文件位於同一文件夾中。 popup.js應該具有displayFormContents()函數。 在表單提交時調用該函數。

<!DOCTYPE html>
<html>
<head>
    <script src="popup.js" type="text/javascript"></script>
</head>
<body>
<form onsubmit="displayFormContents()">
<p id="demo"></p>
First name:<br>
<input type="text" name="firstname" >
<br>
Name:<br>
<input type="text" name="name" >
<br>
    age : <input type="date" name="Age">
    Phone : <input type="number" name="Phone">
    email : <input type="email" name="mail">
    Student at UCLA? : <input type="checkbox" name="Doe">
    <input type="radio" name="gender" value="male" checked> Male<br>
    <input type="radio" name="gender" value="female"> Female<br>
    <input type="radio" name="gender" value="other"> Other
<input type="submit" value="Submit"/>
</form>

</body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM