gpt4 book ai didi

javascript - JavaScript 中的 Alert() 循环

转载 作者:行者123 更新时间:2023-11-28 13:35:27 25 4
gpt4 key购买 nike

我对 JS 非常陌生,但现在我正在尝试创建一个表单验证方法,该方法不断检查所有条目是否不等于 NaN 或 NULL(我将其描述为“”) 。但是我认为我错误地使用了“alert()”函数,并且它不会停止检查足够长的时间以便有人重新输入信息。

我的逻辑是保留在“checkblanks”函数中,同时返回 bool 值“false”,但我的警报消息不会停止在屏幕上显示。

<小时/>

enter image description here

然后,当我通过 Firefox 强制“停止”警报并填写 x1、x2、x3 字段时,它会打印两次 operations 函数。

<小时/>

enter image description here

我的JS

//Direction Toggle
$("p, #answer").hide();

$("h1").click(function () {
$(this).next().slideToggle(300);
});

function testResults(form) {

//Form validation
while (checkblanks(form) == false) {
alert("Please fill in all numeric values");
checkblanks(form);
}

function checkblanks(pform1) {
var display = "";

if (pform1.x1.value == "" || pform1.x1.value == NaN) {
display += "x1, ";
}

if (pform1.x2.value == "" || pform1.x2.value == NaN) {
display += "x2, ";
}

if (pform1.x3.value == "" || pform1.x3.value == NaN) {
display += "x3";
}
if (display != "") {
return false;
} else {
return true;
}
}

//Complete operations if you're all set!
operations(form);

function operations(form) {
//JQuery Answer Show
$(".button").click(function () { //after form submission
$(".matrix").slideUp(1000, function () { //hiding the matrix form
$("#answer").slideDown(1000); //and display the answer
});
});

//System Class Local to operations function
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)));
};
}

//Assign x1-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;

//Pass to constructor and calculate
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
var out = document.getElementById('real-answer');
out.innerHTML += "<b>The equations you entered were:</b>" + "<br />" + D.x1 + "x + " + D.y1 + "y + " + D.z1 + "z = " + D.a1 + "<br />" + D.x2 + "x + " + D.y2 + "y + " + D.z2 + "z = " + D.a2 + "<br />" + D.x3 + "x + " + D.y3 + "y + " + D.z3 + "z = " + D.a3 + "<br /><br />" +

"The answer for <b>D</b> is " + D.calcDanswer() + "<br />" +
"The answer for <b>Dx</b> is " + D.calcXanswer() + "<br />" +
"The answer for <b>Dy</b> is " + D.calcYanswer() + "<br />" +
"The answer for <b>Dz</b> is " + D.calcZanswer() + "<br />" +
"<b>X</b> is " + X + "<br />" +
"<b>Y</b> is " + Y + "<br />" +
"<b>Z</b> is " + Z + "<br />" + "<br />";
}
}

我的 HTML

<body>
<!--DIRECTIONS & FORM-->
<div class="matrix">
<h1><span id="highlight">How Does This Work?</span></h1>

<p>Type in all the information for your system of three equations.
<br />When finished, hit "Go".</p>
<!--Form-->
<FORM NAME="myform" id="form" 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" class="button" name="button" value="GO" onClick="testResults(this.form)" />
</FORM>
<!--....................................-->
</div>
<!--ANSWER-->
<div id="answer">
<h1><span id="highlight">The Answer:</span></h1>

<div id='real-answer'></div>
</div>
</body>

不幸的是,我创建的 jsfiddle 没有显示我的浏览器显示网页的方式,但这是我的代码:http://jsfiddle.net/Xqd5W/

最佳答案

<input type="number" required />

问题已解决。

关于javascript - JavaScript 中的 Alert() 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21768960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com