简体   繁体   English

HTML按钮onclick事件

[英]HTML button onclick event

3个按钮3点击事件

This might be a very basic question; 这可能是一个非常基本的问题; believe me I found very hard to find the answer to this question on the internet. 相信我,我发现很难在互联网上找到这个问题的答案。 I have 3 HTML pages stored in my server (Tomcat locally) & i want to open these HTML pages on a button click. 我在我的服务器(本地Tomcat)中存储了3个HTML页面,我想在按钮点击时打开这些HTML页面。 Any help would be highly appreciated. 任何帮助将受到高度赞赏。

Here is my code, 这是我的代码,

<!DOCTYPE html>
<html>
  <head>
      <meta charset="ISO-8859-1">
      <title>Online Student Portal</title>
  </head>
<body>
  <form action="">
      <h2>Select Your Choice..</h2>
      <input type="button" value="Add Students">
      <input type="button" value="Add Courses">
      <input type="button" value="Student Payments">
  </form>
</body>
</html>

Try this:- 试试这个:-

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="ISO-8859-1"> 
    <title>Online Student Portal</title> 
</head> 
<body> 
    <form action="">
         <input type="button" value="Add Students" onclick="window.location.href='Students.html';"/>
         <input type="button" value="Add Courses" onclick="window.location.href='Courses.html';"/>
         <input type="button" value="Student Payments" onclick="window.location.href='Payment.html';"/>
    </form> 
</body> 
</html>

You should all know this is inline scripting and is not a good practice at all...with that said you should definitively use javascript or jQuery for this type of thing: 你应该都知道这是内联脚本并且根本不是一个好习惯......据说你应该明确地使用javascript或jQuery来做这类事情:

HTML HTML

<!DOCTYPE html> 
<html> 
 <head> 
    <meta charset="ISO-8859-1"> 
    <title>Online Student Portal</title> 
 </head> 
 <body> 
    <form action="">
         <input type="button" id="myButton" value="Add"/>
    </form> 
 </body> 
</html>

JQuery JQuery的

var button_my_button = "#myButton";
$(button_my_button).click(function(){
 window.location.href='Students.html';
});

Javascript 使用Javascript

  //get a reference to the element
  var myBtn = document.getElementById('myButton');

  //add event listener
  myBtn.addEventListener('click', function(event) {
    window.location.href='Students.html';
  });

See comments why avoid inline scripting and also why inline scripting is bad 请参阅注释,以避免内联脚本以及内联脚本编写错误的原因

on first button add the following. 在第一个按钮上添加以下内容。

onclick="window.location.href='Students.html';"

similarly do the rest 2 buttons. 同样做其余的2个按钮。

<input type="button" value="Add Students" onclick="window.location.href='Students.html';"> 
<input type="button" value="Add Courses" onclick="window.location.href='Courses.html';"> 
<input type="button" value="Student Payments" onclick="window.location.href='Payments.html';">

This example will help you: 这个例子可以帮助你:

<form>
    <input type="button" value="Open Window" onclick="window.open('http://www.google.com')">
</form>

You can open next page on same page by: 您可以在同一页面上打开下一页:

<input type="button" value="Open Window" onclick="window.open('http://www.google.com','_self')">

Having trouble with a button onclick event in jsfiddle? jsfiddle中的按钮onclick事件有问题?

If so see Onclick event not firing on jsfiddle.net 如果是这样,请参阅在jsfiddle.net上触发的Onclick事件

<body>
"button" value="Add Students" onclick="window.location.href='Students.html';"> 
<input type="button" value="Add Courses" onclick="window.location.href='Courses.html';"> 
<input type="button" value="Student Payments" onclick="window.location.href='Payments.html';">
</body>

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

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