首页 > 解决方案 > HTML 测验 - JavaScript 按钮(将结果变为绿色,假红色)

问题描述

注意:这只是我必须编写的代码片段。大约有 8 个问题,所有问题都具有相同的结构,只是带有适当的“文本”

我的问题是,我有一个 HTML 测验,如果我点击下面的按钮,我需要正确/错误的答案来相应地改变颜色。我是 JS 的新手,如果之前已经回答过,我很抱歉。我花了几个小时试图找到解决方案,并询问了我的一位同学。我觉得我错过了一些对其他人来说可能很明显的东西。

 /*  What I've already tried:
 x Instead of using "" - used those ''
 x Tried using the div ID 
 x I added "var" infront of the document.getElement(...) and removed it again
 x Tried document.getElement(..).style.Texttransform.color=".." */





/*The HTML Part*/

<div id= "question7">

<form>
<p> Question 7? </p> 

<p><input type = "radio" id = "wrong" name= "question7a" value = "Yes">
 Yes a b c <br></p>

<p><input type = "radio" id = "wrong" name= "question7b" value = "No"
>No, a b c <br></p>

</form>
</div>



/* The Button */

<div id= "send" >

<form>
<input id="change_button" type="button" value="Change" 
onClick="change();"/>

</form>
</div>




/*The Script Part*/

<script>
function change() {

document.getElementbyID("wrong").style.color ="red";
document.getElementbyID("right").style.color ="green";

}
</script>

标签: javascripthtmlradio-button

解决方案


<div id= "question7">

<form>
<p> Question 7? </p> 

<p id = "wrong" ><input type = "radio"  name= "question7a" value = "Yes">
 Yes a b c </input></p>

<p id = "right"  ><input type = "radio"  name= "question7b" value = "No"
>No, a b c </input></p>

</form>
</div>


<div id= "send" >

<form>
<input id="change_button" type="button" value="Change" 
onClick="change()"/>

</form>
</div>

确切的语法是document.getElementById(notice to ById) 然后把id="right"and id ="wrong"in<p>这样你就完成了。 检查此代码


推荐阅读