简体   繁体   English

JavaScript不从HTML表单中获取值

[英]JavaScript not taking values from HTML form

I'm currently trying to make a project that solves Kremer's rule, however, my JS is not printing to the browser. 我目前正在尝试制作一个解决Kremer规则的项目,但是,我的JS并没有打印到浏览器。 This is what I have so far: 这是我到目前为止:

I was wondering how I would fiddle with my code in over to print to the browser: Am I using the wrong method? 我想知道如何在打印到浏览器时使用我的代码:我使用了错误的方法吗? Should I use the "getElementById()" function to get the values instead. 我应该使用“getElementById()”函数来获取值。 Is there an issue declaring a class inside a function? 是否存在在函数内声明类的问题?

It is only writing "document.write("The equations are:");" 它只写“document.write(”方程是:“);” to the document above the form before the function is even called. 甚至在调用函数之前到表单上方的文档。

<!DOCTYPE html>
<html>
<head>
    <title>Kremer's rule</title>
    <link type="text/css" rel="stylesheet" href="stylesheet.css" />
    <h1>Solve Kremer's Rule</h1>

    <SCRIPT LANGUAGE="JavaScript">
    function testResults (form) {

        function system (x1, x2, x3, y1, y2, y3, z1, z2, z3, a1, a2, a3){
           this.x1 = x1;
           this.x2 = x2;
           this.x3 = x3;
           this.y1 = y1;
           this.y2 = y2;
           this.y3 = y3;
           this.z1 = z1;
           this.z2 = z2;
           this.z3 = z3;
           this.a1 = a1;
           this.a2 = a2;
           this.a3 = a3;
           this.calcDanswer = function() {
               return (this.x1*((this.y2*this.z3)-(this.z2*this.y3))) - (this.y1*((this.x2*this.z3)-(this.z2*this.x3))) + (this.z1*((this.x2*D.y3)- (this.y2*this.x3)));
           };
           this.calcXanswer = function(){
               return (this.a1*((this.y2*this.z3)-(this.z2*this.y3))) - (this.y1*((this.a2*this.z3)-(this.z2*this.a3))) + (this.z1*((this.a2*this.y3)-(this.y2*this.a3)));
           };
           this.calcYanswer = function(){
               return (this.x1*((this.a2*this.z3)-(this.z2*this.a3))) - (this.a1*((this.x2*this.z3)-(this.z2*this.x3))) + (this.z1*((this.x2*this.a3)-(this.a2*this.x3)));
           };
           this.calcZanswer = function(){
               return (this.x1*((this.y2*this.a3)-(this.a2*this.y3))) - (this.y1*((this.x2*this.a3)-(this.a2*this.x3))) + (this.a1*((this.x2*this.y3)-(this.y2*this.x3)));
           };
        }

        var x1 = form.x1.value;
        var x2 = form.x2.value;
        var x3 = form.x3.value;
        var y1 = form.y1.value;
        var y2 = form.y2.value;
        var y3 = form.y3.value;
        var z1 = form.z1.value;
        var z2 = form.z2.value;
        var z3 = form.z3.value;
        var a1 = form.a1.value;
        var a2 = form.a2.value;
        var a3 = form.a3.value;

        var D = new system(x1, x2, x3, y1, y2, y3, z1, z2, z3, a1, a2, a3);
        var X = D.calcXanswer()/D.calcDanswer();
        var Y = D.calcYanswer()/D.calcDanswer();
        var Z = D.calcZanswer()/D.calcDanswer();
    }

      //printing to console
       document.write("The equations are:");
       document.write(D.x1 + "x + " + D.y1 + "y + " + D.z1 +"z = "+D.a1);
       document.write(D.x2 + "x + " + D.y2 + "y + " + D.z2 +"z = "+D.a2);
       document.write(D.x3 + "x + " + D.y3 + "y + " + D.z3 +"z = "+D.a3);

       document.write("The answer for D is " + D.calcDanswer());
       document.write("The answer for Dx is " + D.calcXanswer());
       document.write("The answer for Dy is " + D.calcYanswer());
       document.write("The answer for Dy is " + D.calcZanswer());
       document.write();
       document.write("X is " + X);
       document.write("Y is " + Y);
       document.write("Z is " + Z);
    </SCRIPT>

</head>

<body>
    <!--Explaination-->
    <div>
        <h2><span id="highlight">What this does</span></h2>
        <p>Type in all the information for your system of three equations.<br />
        When finished hit "Go".</p>
    </div>

    <!--Form-->
    <div class="matrix">
        <FORM NAME="myform" ACTION="" METHOD="GET">
        <input type="text" name="x1"> x + <input type="text" name="y1"> y + <input type="text" name="z1"> z = <input type="text" name="a1"><br />
        <input type="text" name="x2"> x + <input type="text" name="y2"> y + <input type="text" name="z2"> z = <input type="text" name="a2"><br />
        <input type="text" name="x3"> x + <input type="text" name="y3"> y + <input type="text" name="z3"> z = <input type="text" name="a3"><br />
        <INPUT TYPE="button" NAME="button" Value="Click" onClick="testResults(this.form)">
        </form>
    </div>

    <!--Answer-->
    <div id="answer">
        <h1><span id="highlight">The Answer:</span></h2>
            hiyhioiihhoihohhi<br/>
    </div>
</body>

</html>

Your D,X,Y,Z variables are local to testResults and you are trying to use them outside of testResults . 您的D,X,Y,Z变量是testResults本地变量,并且您尝试在testResults之外使用它们。

You probably just want to move your document.write s inside of testResults , better yet, manipulate the DOM instead so you don't lose all the content from the page 您可能只想将document.write移动到testResults内部,更好的是,操纵DOM,这样您就不会丢失页面中的所有内容

Working code 工作代码

function testResults (form) {

    function system (x1, x2, x3, y1, y2, y3, z1, z2, z3, a1, a2, a3){
        ...
    }

    var x1 = form.x1.value;
    var x2 = form.x2.value;
    var x3 = form.x3.value;
    var y1 = form.y1.value;
    var y2 = form.y2.value;
    var y3 = form.y3.value;
    var z1 = form.z1.value;
    var z2 = form.z2.value;
    var z3 = form.z3.value;
    var a1 = form.a1.value;
    var a2 = form.a2.value;
    var a3 = form.a3.value;

    var D = new system(x1, x2, x3, y1, y2, y3, z1, z2, z3, a1, a2, a3);
    var X = D.calcXanswer()/D.calcDanswer();
    var Y = D.calcYanswer()/D.calcDanswer();
    var Z = D.calcZanswer()/D.calcDanswer();



    // printing to he apge
    var out = document.getElementById('real-answer');
    out.innerHTML += "The equations are:" +
        D.x1 + "x + " + D.y1 + "y + " + D.z1 +"z = "+D.a1 +
        D.x2 + "x + " + D.y2 + "y + " + D.z2 +"z = "+D.a2 +
        D.x3 + "x + " + D.y3 + "y + " + D.z3 +"z = "+D.a3 +

       "The answer for D is " + D.calcDanswer() + 
       "The answer for Dx is " + D.calcXanswer() +
       "The answer for Dy is " + D.calcYanswer() + 
       "The answer for Dy is " + D.calcZanswer() +
       "X is " + X +
       "Y is " + Y +
       "Z is " + Z;        
}

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

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