0

2 つのテキスト ボックスとボタンがある HTML ページがあります。結果を Textbox に表示する Compute 関数を作成しましたが、機能しません。ページでアラートが発生していません。

ここで私を助けてくれる人はいますか、どこが間違っていますか。

<script type="text/javascript">
    function isNumericKey(e)
    {
        if (window.event) { 
        var charCode = window.event.keyCode;
        }
        else if (e) 
        { 
        var charCode = e.which; 
        }
        else 
        { 
        return true; 
        }
        if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
        }
        return true;
    }
    function submitMyNumber()
    {
      alert(1);
           var x = document.getElementById('myInput').value;
        return x.match(/^[0-9]+$/) != null;
        var y = document.getElementById('myInput1').value;
        return y.match(/^[0-9]+$/) != null;
        compute();
    }
    function compute()
    {
       alert(1);
    var a,b,c;
    var qtc=document.form1.myInput.value;
        alert(qtc);
    var hr=document.getByElementById('myinput1').value;
        alert(hr);  
       a=60/hr;
       alert(a);
    b=math.sqrt(a);
      alert(b);
    c=qtc/b;
       alert(c);
       document.write(a + "" + b+ " " + c+ "")
    }
       </script>
     </head>
     <body>
    <form>
    <table align="center" style="border:2px solid black">
        <tr>
            <td>Qtc:</td>
            <td><input type="text" id="myInput" name="myInput" onkeypress="return isNumericKey(event);" /><br /></td></tr>
            <tr>
                <td>Hr:</td>
                <td><input type="text" id="myInput1" name="myInput1" onkeypress="return isNumericKey(event);" /><br />
                </td>
            </tr>
                <tr>
                <td> <input type="submit" id="mySubmit" name="mySubmit" value="Submit only Number" onclick="compute()" />
                </td>
            </tr>
       <tr><td><input type="text" id="result" name="result" value="" /></td></tr>
    </table>
    </form>
</body>
</html>
4

7 に答える 7

0

returnステートメントをifステートメントに変更します。

function submitMyNumber()
{
  alert(1);
       var x = document.getElementById('myInput').value;
    if (x.match(/^[0-9]+$/) === null ) {
        return;
    }

    var y = document.getElementById('myInput1').value;
    if (y.match(/^[0-9]+$/) === null ) {
        return;
    }

    compute();
}
于 2012-10-12T07:27:50.197 に答える
0

関数は次のようになります。

function submitMyNumber() {
    var x = document.getElementById('myInput').value;
    var x_match=  x.match(/^[0-9]+$/) != null;
    //Store x_match in hiddenfield

    var y = document.getElementById('myInput1').value;
    var y_match= y.match(/^[0-9]+$/) != null;
     //Store y_match in hiddenfield

    compute();
    return;
}
于 2012-10-12T07:32:46.343 に答える
0

コードの間違いの可能性

  • 計算関数が呼び出されることはありません
  • 結果テキスト ボックスの値が設定されない
于 2012-10-12T07:24:11.120 に答える
0

コードにフォーム名がありません:

これを次から変更します<form>

<form name = "form1" >

PS:次のようなものを使用する場合document.form1.myInput.value;

myInput名前のフォームから値を取得するように指示しますform1

于 2012-10-12T07:24:34.163 に答える
0

コードにタイプミスがあります: 更新: Fiddle

  1. compute()機能中

    getByElementById('myinput1')あるべきgetElementById('myInput1') math.sqrt(a);Math.sqrt(a);

関数は次のようになります

function compute(){
    var a,b,c;
    var qtc=document.form1.myInput.value;
    alert(qtc);
    var hr=document.getElementById('myInput1').value;
    alert(hr);  
    a=60/hr;
    alert(a);
    b=Math.sqrt(a);
    alert(b);
    c=qtc/b;
    alert(c);
    document.write(a + "" + b+ " " + c+ "")
    //var result = document.getElementById('result');   
    //result.value = c;                
}
  1. HTMLformには name=form1 が必要です<form name="form1">

    <form name="form1">
        <table align="center" style="border:2px solid black">
        <tr> 
           <td>Qtc:</td>
           <td><input type="text" id="myInput" name="myInput" onkeypress="return isNumericKey(event);" /><br /></td>
        </tr>
        <tr>
           <td>Hr:</td>
           <td><input type="text" id="myInput1" name="myInput1" onkeypress="return isNumericKey(event);" /><br /></td>
        </tr>
        <tr>
           <td><button type="button" onclick="compute(); return false;">Submit only Number</button></td>
       </tr>
       <tr><td><input type="text" id="result" name="result" value="" /></td></tr>
       </table>
      </form>
    
于 2012-10-12T07:47:42.307 に答える