如何使用Javascript处理HTML单选按钮? 代数评论员 氢 { 颜色:白色; 字体系列:verdana; 文本阴影:黑色0.1em 0.1em 0.2em; 文本对齐:居中; } 桌子 { 字体系列:verdana; 颜色:白色; 文本阴影:黑色0.1em 0.1em 0.2em; } 功能问题1() { 如果(“quick.question1.value=='d'”) { 警惕(“这是正确的答案!”); } 其他的 { 警报(“哎呀!再试一次!”); } } 这里有10个问题需要你回答。你可能需要一份草稿纸,所以在接受这个审稿人之前先准备一份。 通过点(3,8)并平行于线x-3y=5的线的方程是什么 y=1/3X+5 y=3x+7 y=3/5X+3 y=1/3 x+7 您从不定义测验,因此当您尝试为测验.question1.value赋值时,代码会抛出一个错误 您的if语句正在测试一个字符串是否为真值,它总是这样。你引用的话太多了
首先,您缺少一个表单元素 这需要为JavaScript代码的工作提供支持如何使用Javascript处理HTML单选按钮? 代数评论员 氢 { 颜色:白色; 字体系列:verdana; 文本阴影:黑色0.1em 0.1em 0.2em; 文本对齐:居中; } 桌子 { 字体系列:verdana; 颜色:白色; 文本阴影:黑色0.1em 0.1em 0.2em; } 功能问题1() { 如果(“quick.question1.value=='d'”) { 警惕(“这是正确的答案!”); } 其他的 { 警报(“哎呀!再试一次!”); } } 这里有10个问题需要你回答。你可能需要一份草稿纸,所以在接受这个审稿人之前先准备一份。 通过点(3,8)并平行于线x-3y=5的线的方程是什么 y=1/3X+5 y=3x+7 y=3/5X+3 y=1/3 x+7 您从不定义测验,因此当您尝试为测验.question1.value赋值时,代码会抛出一个错误 您的if语句正在测试一个字符串是否为真值,它总是这样。你引用的话太多了,javascript,html,css,Javascript,Html,Css,首先,您缺少一个表单元素 ... 这需要为JavaScript代码的工作提供支持 单选按钮的名称属性和提交按钮需要不同 我想单选按钮和提交按钮的name属性不应该相同。 尝试更改submit按钮的name属性。首先,将radiosvalue属性设置为选项。在html中修改以下内容 <html> <head> <title> Algebra Reviewer </title> <style type="text/css"> h2 {
单选按钮的名称
属性
和提交按钮
需要不同我想单选按钮和提交按钮的
name
属性不应该相同。
尝试更改submit按钮的
name
属性。首先,将radiosvalue
属性设置为选项。在html中修改以下内容
<html>
<head>
<title> Algebra Reviewer </title>
<style type="text/css">
h2
{
color: white;
font-family: verdana;
text-shadow: black 0.1em 0.1em 0.2em;
text-align: center;
}
table
{
font-family:verdana;
color: white;
text-shadow: black 0.1em 0.1em 0.2em;
}
</style>
<script type="text/javascript">
function question1()
{
if ("quiz.question1.value=='d'")
{
alert ("That's the correct answer!");
}
else
{
alert ("Oops! try again!");
}
}
</script>
</head>
<body>
</br>
<h2>
Here are 10 items for you to answer. You might need scratch paper- so get one before taking this reviewer.
</h2>
</br>
</br>
<center>
<table border="0" bgcolor="tan">
<tr> <td>
<ol>
<li> What is the equation of the line passing through point (3,8) and parallel to the line x - 3y = 5 ? </li>
</br>
</br>
<input type = "radio" name = "question1" onclick = "quiz.question1.value=='a'"/> y = 1/3 x + 5
</br>
<input type = "radio" name = "question1" onclick = "quiz.question1.value=='b'"/> y = 3x + 7
</br>
<input type = "radio" name = "question1" onclick = "quiz.question1.value=='c'"/> y = 3/5 x + 3
</br>
<input type = "radio" name = "question1" onclick = "quiz.question1.value=='d'"/> y = 1/3 x + 7
</br>
</br>
<input type = "button" onclick = "question1()" name = "question1" value = "Submit"/>
<br/>
<br/>
</td> </tr> </table> </center>
</body>
</html
y=1/3x+5
y=3x+7
y=3/5X+3
y=1/3 x+7
并按如下方式更改功能:
<input type = "radio" name = "question1" value='a'/> y = 1/3 x + 5
</br>
<input type = "radio" name = "question1" value='b'/> y = 3x + 7
</br>
<input type = "radio" name = "question1" value='c'/> y = 3/5 x + 3
</br>
<input type = "radio" name = "question1" value='d'/> y = 1/3 x + 7
功能问题1()
{
选择变量;
var questions=document.getElementsByName(“问题1”);
对于(var i=0;i
您从未定义测验变量。使用“值”属性获取选定单选按钮。使用以下代码
function question1()
{
var selectedAns;
var questions = document.getElementsByName("question1");
for(var i = 0; i < questions.length; i++) {
if(questions[i].checked == true) {
selectedAns = questions[i].value;
break;
}
if(selectedAns== 'd')
alert ("That's the correct answer!");
}
else
{
alert ("Oops! try again!");
}
}
代数评论员
氢{
颜色:白色;
字体系列:verdana;
文本阴影:黑色0.1em 0.1em 0.2em;
文本对齐:居中;
}
桌子{
字体系列:verdana;
颜色:白色;
文本阴影:黑色0.1em 0.1em 0.2em;
}
功能问题1(){
var option=document.querySelector('input[name=“question1”]:checked')。值;
如果(选项=='d'){
警惕(“这是正确的答案!”);
}
否则{
警报(“哎呀!再试一次!”);
}
}
这里有10个问题需要你回答。你可能需要一份草稿纸,所以在接受这个审稿人之前先准备一份。
通过点(3,8)并平行于线x-3y=5的线的方程是什么李>
y=1/3X+5
y=3x+7
y=3/5X+3
y=1/3 x+7
好的,我通过使用隐藏字段修复了您的问题。对于初学者来说,这是很好的努力。但是,这不是最好的方法。无论如何,继续努力。你会到达那里的
<html>
<head>
<title> Algebra Reviewer </title>
<style type="text/css">
h2 {
color: white;
font-family: verdana;
text-shadow: black 0.1em 0.1em 0.2em;
text-align: center;
}
table {
font-family:verdana;
color: white;
text-shadow: black 0.1em 0.1em 0.2em;
}
</style>
<script type="text/javascript">
function question1() {
var option = document.querySelector('input[name = "question1"]:checked').value;
if (option == 'd') {
alert("That's the correct answer!");
}
else {
alert ("Oops! try again!");
}
}
</script>
</head>
<body>
</br>
<h2>
Here are 10 items for you to answer. You might need scratch paper- so get one before taking this reviewer.
</h2>
</br>
</br>
<center>
<table border="0" bgcolor="tan">
<tr>
<td>
<ol>
<li> What is the equation of the line passing through point (3,8) and parallel to the line x - 3y = 5 ? </li>
</br>
</br>
<input type = "radio" name = "question1" value="a" /> y = 1/3 x + 5
</br>
<input type = "radio" name = "question1" value="b" /> y = 3x + 7
</br>
<input type = "radio" name = "question1" value="c" /> y = 3/5 x + 3
</br>
<input type = "radio" name = "question1" value="d" /> y = 1/3 x + 7
</br>
</br>
<input type = "button" onclick = "question1()" name = "question1" value = "Submit"/>
<br/>
<br/>
</td>
</tr>
</table>
</center>
</body>
</html>
代数评论员
氢
{
颜色:白色;
字体系列:verdana;
文本阴影:黑色0.1em 0.1em 0.2em;
文本对齐:居中;
}
桌子
{
字体系列:verdana;
颜色:白色;
文本阴影:黑色0.1em 0.1em 0.2em;
}
功能问题1()
{
if(document.getElementById('question1Answer')。值='d')
{
警惕(“这是正确的答案!”);
}
其他的
{
警报(“哎呀!再试一次!”);
document.getElementById('question1Answer')。值=“”
}
}
这里有10个问题需要你回答。你可能需要一份草稿纸,所以在接受这个审稿人之前先准备一份。
通过点(3,8)并平行于线x-3y=5的线的方程是什么李>
y=1/3X+5
y=3x+7
y=3/5X+3
y=1/3 x+7
为laout使用和停止使用表。请完全无关,但我认为服务器应该验证正确答案,而不是javascript。如果你不这样做,你的学生将很容易作弊,在源html代码中寻找答案。但是要小心。这在IE7中不起作用(不过,IE7用于
<html>
<head>
<title> Algebra Reviewer </title>
<style type="text/css">
h2
{
color: white;
font-family: verdana;
text-shadow: black 0.1em 0.1em 0.2em;
text-align: center;
}
table
{
font-family:verdana;
color: white;
text-shadow: black 0.1em 0.1em 0.2em;
}
</style>
<script type="text/javascript">
function question1()
{
if (document.getElementById('question1Answer').value =='d')
{
alert ("That's the correct answer!");
}
else
{
alert ("Oops! try again!");
document.getElementById('question1Answer').value =''
}
}
</script>
</head>
<body>
</br>
<h2>
Here are 10 items for you to answer. You might need scratch paper- so get one before taking this reviewer.
</h2>
</br>
</br>
<center>
<table border="0" bgcolor="tan">
<tr> <td>
<ol>
<li> What is the equation of the line passing through point (3,8) and parallel to the line x - 3y = 5 ? </li>
</br>
</br>
<input type = "radio" name = "question1" onclick = "document.getElementById('question1Answer').value ='a'"/> y = 1/3 x + 5
</br>
<input type = "radio" name = "question1" onclick = "document.getElementById('question1Answer').value ='b'"/> y = 3x + 7
</br>
<input type = "radio" name = "question1" onclick = "document.getElementById('question1Answer').value ='c'"/> y = 3/5 x + 3
</br>
<input type = "radio" name = "question1" onclick = "document.getElementById('question1Answer').value ='d'"/> y = 1/3 x + 7
</br>
</br>
<input type = "hidden" id = "question1Answer" value = ""/>
<input type = "button" onclick = "question1()" name = "question1" value = "Submit"/>
<br/>
<br/>
</td> </tr> </table> </center>
</body>
</html>