26

3 つのボタン 3 つの onclick イベント

これは非常に基本的な質問かもしれません。この質問に対する答えをインターネットで見つけるのは非常に難しいと思いました。サーバー (Tomcat ローカル) に 3 つの HTML ページが保存されており、ボタンをクリックするだけでこれらの HTML ページを開きたいと考えています。どんな助けでも大歓迎です。

ここに私のコードがあります、

<!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>
4

6 に答える 6

50

これを試して:-

<!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>
于 2013-09-08T04:04:54.543 に答える
27

これはインライン スクリプトであり、まったく適切な方法ではないことをすべて知っておく必要があります...とはいえ、この種のことには javascript または jQuery を確実に使用する必要があります。

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

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

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';
  });

インライン スクリプトを避ける理由と、インライン スクリプトが悪い理由のコメントを参照してください。

于 2014-05-12T18:11:14.830 に答える
7

最初のボタンに以下を追加します。

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

残りの 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';">
于 2013-09-08T04:04:23.510 に答える
2

jsfiddle のボタン onclick イベントに問題がありますか?

その場合は、jsfiddle.net で Onclick イベントが発生しないことを確認してください。

于 2015-04-02T03:46:48.897 に答える
2

この例は次のことに役立ちます。

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

次の方法で同じページの次のページを開くことができます。

<input type="button" value="Open Window" onclick="window.open('http://www.google.com','_self')">
于 2013-09-08T04:02:45.543 に答える
1
<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>
于 2014-04-17T12:52:19.440 に答える