gpt4 book ai didi

javascript - 获取所选单选按钮的组合以在单击按钮时显示消息

转载 作者:太空宇宙 更新时间:2023-11-04 12:40:57 25 4
gpt4 key购买 nike

我有多种单选按钮,根据选择它们的组合,当用户对输入感到满意时按下按钮时,我希望在 span 元素中显示一条消息。然而,它并没有实现这一点。这是我的代码到目前为止的样子:

HTML:

<p>Select your first colour:</p>
<input type="radio" id="red" name="primary" value="0">Red<br>
<input type="radio" id="yellow" name="primary" value="1">Yellow<br>
<input type="radio" id="blue" name="primary" value="2">Blue<br>

<p>Select your second colour:</p>
<input type="radio" id="green" name="other" value="0">Green<br>
<input type="radio" id="white" name="other" value="1">White<br>
<input type="radio" id="purple" name="other" value="2">Purple<br>

<input type="button" id="mixColours" value="Mix!"><br />
<span id="colourResult"> </span><br /><br />

Javascript:

var $ = function(id) {
return document.getElementById(id);
};

var red = $("red");
var yellow = $("yellow");
var blue = $("blue");
var green = $("green");
var white = $("white");
var purple = $("purple");

var mixColours = function mixColours () {

if (red.checked && green.checked) {
$("colourResult").innerHTML = "You made Yellow!";
}
else if (red.checked && white.checked) {
$("colourResult").innerHTML = "You made Pink!";
}
else if (red.checked && purple.checked) {
$("colourResult").innerHTML = "You made Mauve!";
}

if (yellow.checked && green.checked) {
$("colourResult").innerHTML = "You made Yellow!";
}
else if (yellow.checked && white.checked) {
$("colourResult").innerHTML = "You made Light Yellow!";
}
window.onload = function () {
$("mixColours").onclick = mixColours;

或在此处使用 jsfiddle:http://jsfiddle.net/e2yjerm3/

如果有人能指出我的错误并帮助我(或引导我帮助文章),我将不胜感激。我根据教程等创建了这个。我读过,但我似乎误入歧途了。

最佳答案

演示 - http://jsfiddle.net/victor_007/e2yjerm3/3/

2 } } 最后丢失了

var $ = function(id) {
return document.getElementById(id);
};

var red = $("red");
var yellow = $("yellow");
var blue = $("blue");
var green = $("green");
var white = $("white");
var purple = $("purple");

var mixColours = function mixColours() {

if (red.checked && green.checked) {
$("colourResult").innerHTML = "You made Yellow!";
} else if (red.checked && white.checked) {
$("colourResult").innerHTML = "You made Pink!";
} else if (red.checked && purple.checked) {
$("colourResult").innerHTML = "You made Mauve!";
}

if (yellow.checked && green.checked) {
$("colourResult").innerHTML = "You made Yellow!";
} else if (yellow.checked && white.checked) {
$("colourResult").innerHTML = "You made Light Yellow!";
}

}; /* this */
window.onload = function() {
$("mixColours").onclick = mixColours;
}; /* this */
<p>Select your first colour:</p>
<input type="radio" id="red" name="primary" value="0">Red
<br>
<input type="radio" id="yellow" name="primary" value="1">Yellow
<br>
<input type="radio" id="blue" name="primary" value="2">Blue
<br>
<p>Select your second colour:</p>
<input type="radio" id="green" name="other" value="0">Green
<br>
<input type="radio" id="white" name="other" value="1">White
<br>
<input type="radio" id="purple" name="other" value="2">Purple
<br>
<input type="button" onclick="mixColours()" id="mixColours" value="Mix!">
<br />
<span id="colourResult"> </span>

<br />
<br />

关于javascript - 获取所选单选按钮的组合以在单击按钮时显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26878655/

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