JavaScript不从HTML表单中获取值
我目前正在尝试制作一个解决Kremer规则的项目,但是,我的JS没有打印到浏览器上。这就是我到目前为止所做的: 我想知道我将如何处理我的代码以打印到浏览器: 我用错方法了吗?我应该使用“getElementById()”函数来获取值吗。在函数中声明类是否存在问题 在调用函数之前,它只在表单上方的文档中写入“document.write”(“等式为:”);”JavaScript不从HTML表单中获取值,javascript,html,forms,oop,dom,Javascript,Html,Forms,Oop,Dom,我目前正在尝试制作一个解决Kremer规则的项目,但是,我的JS没有打印到浏览器上。这就是我到目前为止所做的: 我想知道我将如何处理我的代码以打印到浏览器: 我用错方法了吗?我应该使用“getElementById()”函数来获取值吗。在函数中声明类是否存在问题 在调用函数之前,它只在表单上方的文档中写入“document.write”(“等式为:”);” <!DOCTYPE html> <html> <head> <title>Kreme
<!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>
克雷默法则
解克雷默法则
函数测试结果(表格){
功能系统(x1、x2、x3、y1、y2、y3、z1、z2、z3、a1、a2、a3){
这是1.x1=x1;
这是0.x2=x2;
这1.x3=x3;
这是1.y1=y1;
这1.y2=y2;
这1.y3=y3;
这是1.z1=z1;
这1.z2=z2;
这1.z3=z3;
这1.a1=a1;
这1.a2=a2;
这1.a3=a3;
this.calcDanswer=函数(){
返回(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=函数(){
返回(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=函数(){
返回(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=函数(){
返回(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=形式x1.值;
var x2=形式x2.值;
var x3=form.x3.value;
变量y1=形式y1.值;
var y2=form.y2.value;
var y3=形式y3.价值;
var z1=形式z1.值;
var z2=形式z2.值;
var z3=形式z3.值;
var a1=形式a1.值;
var a2=形式a2.值;
var a3=表a3.值;
var D=新系统(x1、x2、x3、y1、y2、y3、z1、z2、z3、a1、a2、a3);
var X=D.calcXanswer()/D.calcDanswer();
变量Y=D.calcYanswer()/D.calcDanswer();
var Z=D.calcZanswer()/D.calcDanswer();
}
//打印到控制台
写下(“方程式为:”);
文件写入(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”);
document.write(“D的答案是”+D.calcDanswer());
document.write(“Dx的答案是”+D.calcXanswer());
document.write(“Dy的答案是”+D.calcYanswer());
document.write(“Dy的答案是”+D.calcZanswer());
document.write();
文件。写入(“X是”+X);
文件。写入(“Y是”+Y);
文件。写入(“Z是”+Z);
这是干什么的
输入三方程组的所有信息。
完成后点击“开始”
x+y+z=
x+y+z=
x+y+z=
答案是:
HIYHIOIHHOIHHI
您的D、X、Y、Z
变量是testResults
的本地变量,您试图在testResults
之外使用它们
您可能只想移动文档。在testResults
中写入
s,更好的方法是操作DOM,这样您就不会丢失页面中的所有内容
你得到了什么错误?它没有给我一个错误,相反,它只是在调用函数之前将“document.write”(“公式是:”);”写入表单上方的文档。是的,它给了你一个错误,它试图使用未定义的变量。如果您没有使用浏览器控制台/调试器,
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;
}