1

コードが正しく実行されない理由について質問があります。これらのコマンドを適用するときは、できるだけ基本的にする必要があります。変換ボタンを押しても何もありません。そのためのコマンドも必要でしたか? それは宿題です。私は何時間もそれに手を出しています。

編集***

  <html>
<head>

<script type="text/javascript">
<script>
function Convert()
onclick= document.getElementById('Convert')

    var years = document.getElementById("year").value;
    var days = document.getElementById("days").365.25 * years;
    var hours = document.getElementById("hours").(365.25 * 24) * years;
    var minutes = document.getElementById("minutes").(365.25 * 24 * 60) * years;
    var seconds = document.getElementById("seconds").(365.25 * 24 * 60 * 60) * years;

document.getElementById('days').value = days;
document.getElementById('hours').value = hours;
document.getElementById('minutes').value = minutes;
document.getElementById('seconds').value = seconds;



});
    </script>
  </head>
  <body>
    Years: <input type='text' id='years' /> 
    <button id='Convert'onclick= "Convert()" value= "Convert"/> Convert </button>

    Days: <input type='text' id='days' /> 
    Hours: <input type='text' id='hours' /> 
    Minutes: <input type='text' id='minutes' /> 
    Seconds: <input type='text' id='seconds' /> 
  </body>
 </html> 
4

1 に答える 1

2

いくつかのこと(うまくいけば、それらがあなたを再び動かします):

  1. 関数を呼び出すことはありません。onClickボタンにハンドラーを追加します。
  2. 変数のprompt代わりに固定文字列を使用しています。
  3. inputJavaScript でからデータを抽出する必要があります。そのために使用できますdocument.getElementById()

注意してください、私はあなたに答えを与えることができますが、宿題と学習はすべて自分で物事を理解することです. 私のヒントを参考にして、何ができるか見てみましょう。再び行き詰まった場合は、得たもので質問を編集してください。

よし、次のラウンド。関数で行う必要があることConvert:

  1. まず、次のようにフォームから情報を取得します。

     var years = document.getElementById("year").value;
    
  2. 次に、計算しますか。

     var days = 365 * years;
    
  3. 最後に、結果を書き戻します。

    document.getElementById("days").value = days;
    

追加のヒント:

  1. id='Convert'と の間にスペースがありませんonclick
  2. Firebug for Firefoxなどのデバッガーをインストールします。

幸運を!

ラウンド 3; これが完全な答えです。何が起こっているのかを理解するようにしてください。通常、実際の例から学ぶことは良いことです。

私が見つけた余分なもの:

  1. HTMLの追加の<script>タグ
  2. 関数定義が間違っています。function foo() { }

------ 完全な回答が続きます ------

<html>
<head>

<script type="text/javascript">

// Declare a function called Convert()
function Convert() {
    // Get the value of what the user entered in "years"
    var years = document.getElementById("years").value;

    // Calculate all the other values
    var days = years * 365.25;
    var hours = days * 24;
    var minutes = hours * 60;
    var seconds = minutes * 60;

    // Write the results in the input fields    
    document.getElementById('days').value = days;
    document.getElementById('hours').value = hours;
    document.getElementById('minutes').value = minutes;
    document.getElementById('seconds').value = seconds;
}
</script>
</head>
  <body>
    Years: <input type='text' id='years' /> 
    <!-- define a button that will call Convert() when clicked on -->
    <button id='Convert' onclick= "Convert()" value="Convert">Convert</button>

    Days: <input type='text' id='days' /> 
    Hours: <input type='text' id='hours' /> 
    Minutes: <input type='text' id='minutes' /> 
    Seconds: <input type='text' id='seconds' /> 
  </body>
 </html> 
于 2012-10-15T18:43:17.480 に答える