[英]Radio button values and submit buttons
我正在编写一个程序,它需要 3 个问题并确定其中任何一个问题的值是否为 0。然后,当您点击提交时,它会将您发送到下一页,该页面将根据值是 0 还是 1 编写一些内容。如果是其中任何一个都为 0,它应该说是 false,如果它是所有的 1,那么它应该写通过。
当我单击单选按钮时,它会写入错误或正确传递,但这只有在按下提交按钮时才会发生。 当我在选择单选按钮后点击提交按钮时,无论选择什么,它都会更改书面文字以通过。 当我让 onclick function 在不同的页面上写入 pass/false 时,它什么也不写。 我不能只用 JavaScript 做到这一点吗? 我需要能够启动一个新页面,该页面要么有更多问题,要么将结果写入其中。 我试图使用尽可能少的页面。
我在自己的页面上有 HTML 和 JavaScript 文件。
这是 HTML 页面
<form id="myForm">
<fieldset class="article">
<h3>
<legend>Eligibility Test</legend>
</h3>
</fieldset>
<fieldset class="article">
<legend>Have you had your record expunged before?</legend>
<input type="radio" name="field1" value="0" onclick="getscores1(this)" />
<label>Yes</label>
<input type="radio" name="field1" value="1" onclick="getscores1(this)" />
<label>No</label>
</fieldset>
<fieldset class="article">
<legend>Do you have any charges pending against you?</legend>
<input type="radio" name="field2" value="0" onclick="getscores2(this)" />
<label>Yes</label>
<input type="radio" name="field2" value="1" onclick="getscores2(this)" />
<label>No</label>
</fieldset>
<fieldset>
<legend>Is your drivers license suspended?</legend>
<input type="radio" name="field3" value="0" onclick="getscores3(this)"/>
<label>Yes</label>
<input type="radio" name="field3" value="1" onclick="getscores3(this)"/>
<label>No</label>
</fieldset>
<fieldset id="submitbutton" class="article">
<input type="button" id="submit" value="submit" onclick="answer(this)" onclick="location.href ='testSite.html';"/>
</fieldset>
<script src="test.js"></script>
</body>
这就是我在提交按钮转到的 testSite.html 上的内容
这是答案
这是 JavaScript 页面
document.getElementById("submit").onclick = function () {
location.href = "testSite.html";
};
function getscores1(score1) {
answer(score1.value);
}
function getscores2(score2) {
answer(score2.value);
}
function getscores3(score3) {
answer(score3.value);
}
function answer(score1, score2, score3) {
if (score1 == 0 || score2 == 0 || score3 == 0) {
document.getElementById('totalScore').innerHTML = "false";
} else{
document.getElementById('totalScore').innerHTML = "pass";
}
}
谢谢,让我知道是否有任何令人困惑或任何事情,我会解决它。
我对此的最佳猜测是,如果计算错误,可能是因为您将提交输入传递到您的答案 function 中,因此它将 score1 设置为该元素,然后 score2 和 score3 未定义,因此它们也不等于 0 .
将其保存为文件:my_html.html 并在浏览器中运行。 阅读评论
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<span>Total Score: </span> <span id="totalScore"></span>
<form id="myForm">
<fieldset class="article">
<h3><legend>Eligibility Test</legend></h3>
</fieldset>
<fieldset class="article">
<legend>Have you had your record expunged before?</legend> <input type="radio" name="field1" value="0" onclick="getscores1(this)"> <label>Yes</label> <input type="radio" name="field1" value="1" onclick="getscores1(this)"> <label>No</label>
</fieldset>
<fieldset class="article">
<legend>Do you have any charges pending against you?</legend> <input type="radio" name="field2" value="0" onclick="getscores2(this)"> <label>Yes</label> <input type="radio" name="field2" value="1" onclick="getscores2(this)"> <label>No</label>
</fieldset>
<fieldset>
<legend>Is your drivers license suspended?</legend>
<input type="radio" name="field3" value="0" onclick="getscores3(this)"> <label>Yes</label>
<input type="radio" name="field3" value="1" onclick="getscores3(this)"> <label>No</label>
</fieldset>
<fieldset id="submitbutton" class="article">
<input type="button" id="submit" value="submit"> <!-- Don't do this: onclick="location.href ='testSite.html';" -->
</fieldset>
</form>
</body>
<script>
var s1, s2, s3;
[s1, s2, s3] = [0, 0, 0];
document.getElementById("submit").onclick = function () {
//location.href = "testSite.html"; //JavaScript context is on the client side for the current page only. When you reload the page, it gets recreated, all variables are re-initialized.
answer(s1, s2, s3);
};
function getscores1(score1) {
s1 = score1.value;
}
function getscores2(score2) {
s2 = score2.value;
}
function getscores3(score3) {
s3 = score3.value;
}
function answer(score1, score2, score3) {
if (score1 == 0 || score2 == 0 || score3 == 0) {
document.getElementById('totalScore').innerHTML = "false";
} else{
document.getElementById('totalScore').innerHTML = "pass";
}
}
</script>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.