Javascript 单选按钮仅在双击时更改值?
在我的代码中,我有五个单选按钮,其中一个默认选中。单击单选按钮时,会调用函数Javascript 单选按钮仅在双击时更改值?,javascript,html,radio-button,Javascript,Html,Radio Button,在我的代码中,我有五个单选按钮,其中一个默认选中。单击单选按钮时,会调用函数setMode(),但检查选中哪个按钮的测试失败 当第二次单击同一单选按钮调用setMode()时,测试将返回true。 如果(document.getElementById(“rcool”).checked) 我已尝试更改鼠标事件和测试,例如。checked==“true”和。checked==“checked”。 我在jQuery中搜索过,只找到了我不熟悉的类似主题,因此无法理解它们 我正在使用Firefox 47.
setMode()
,但检查选中哪个按钮的测试失败
当第二次单击同一单选按钮调用setMode()
时,测试将返回true。
如果(document.getElementById(“rcool”).checked)
我已尝试更改鼠标事件和测试,例如。checked==“true”
和。checked==“checked”
。
我在jQuery中搜索过,只找到了我不熟悉的类似主题,因此无法理解它们
我正在使用Firefox 47.0,并使用Firebug 2.0.17检查了错误。
我不太清楚如何使用Firebug,也不知道下一步该尝试什么
非常感谢您的帮助或意见
var选项=[“电源=关闭”,“模式=加热”,“温度=20”];
var-temp=20;
函数幂(){
var pwr=document.getElementById(“powerBtn”);
var-pwrtxt;
如果(pwr.innerHTML==“关闭”){
pwrtxt=“电源=打开”;
pwr.innerHTML=“ON”;
}
否则{
pwrtxt=“电源=关闭”;
pwr.innerHTML=“关闭”;
}
选项[0]=pwrtxt;
document.getElementById(“textOutput”).value=选项;
}
函数setMode(){
if(document.getElementById(“rheat”).checked)
选项[1]=“模式=热量”;
if(document.getElementById(“rcool”).checked)
选项[1]=“模式=酷”;
if(document.getElementById(“rdry”).checked)
选项[1]=“模式=干燥”;
if(document.getElementById(“rauto”).已选中)
选项[1]=“模式=自动”;
if(选中document.getElementById(“recono”))
选项[1]=“模式=经济”;
document.getElementById(“textOutput”).value=选项;
}
函数updateTemp(){
var tempString=“temp=”;
选项[2]=tempString.concat(“,temp);
document.getElementById(“textOutput”).value=选项;
}
函数increaseTemp(){
温度+=1;
如果(温度>31){
温度=31;
}
updateTemp();
}
函数decreaseTemp(){
温度-=1;
如果(温度<16){
温度=16;
}
updateTemp();
}
函数stopTime(){
}
函数startTime(){
}
函数incTime(){
}
函数decTime(){
}
关
向上的
向下
加热
酷
干燥
自动
ECONOCOOL
停止时间
启动计时器
增加时间
缩短时间
要在单击单选按钮时启动函数,必须使用onchange事件,而不是onclick/onmouseup。onchange在调用函数之前,请优雅地等待单选按钮被有效单击/更新
var选项=[“电源=关闭”,“模式=加热”,“温度=20”];
var-temp=20;
window.onload=function(){document.getElementById(“textOutput”).value=options;}
函数幂(){
var pwr=document.getElementById(“powerBtn”);
var-pwrtxt;
如果(pwr.innerHTML==“关闭”){
pwrtxt=“电源=打开”;
pwr.innerHTML=“ON”;
}
否则{
pwrtxt=“电源=关闭”;
pwr.innerHTML=“关闭”;
}
选项[0]=pwrtxt;
document.getElementById(“textOutput”).value=选项;
}
函数setMode(){
if(document.getElementById(“rheat”).checked)
选项[1]=“模式=热量”;
if(document.getElementById(“rcool”).checked)
选项[1]=“模式=酷”;
if(document.getElementById(“rdry”).checked)
选项[1]=“模式=干燥”;
if(document.getElementById(“rauto”).已选中)
选项[1]=“模式=自动”;
if(选中document.getElementById(“recono”))
选项[1]=“模式=经济”;
document.getElementById(“textOutput”).value=选项;
}
函数updateTemp(){
var tempString=“temp=”;
选项[2]=tempString.concat(“,temp);
document.getElementById(“textOutput”).value=选项;
}
函数increaseTemp(){
温度+=1;
如果(温度>31){
温度=31;
}
updateTemp();
}
函数decreaseTemp(){
温度-=1;
如果(温度<16){
温度=16;
}
updateTemp();
}
函数stopTime(){
}
函数startTime(){
}
函数incTime(){
}
函数decTime(){
}
关
向上的
向下
加热
酷
干燥
自动
ECONOCOOL
停止时间
启动计时器
增加时间
缩短时间
通常我会将侦听器放在这些单选按钮上,并在单击时识别它们。似乎您更喜欢老式的方式,因此我在代码中没有做太多更改,而是在setMode()
函数中添加了一个名为mode
的参数。基本上,模式
在调用setMode()
时已经定义,因此您不必再检查每个单选按钮<代码>开关()。试试看,祝你好运
var选项=[“电源=关闭”,“模式=加热”,“温度=20”];
var-temp=20;
函数幂(){
var pwr=document.getElementById(“powerBtn”);
var-pwrtxt;
如果(pwr.innerHTML==“关闭”){
pwrtxt=“电源=打开”;
pwr.innerHTML=“ON”;
}
否则{
pwrtxt=“电源=关闭”;
pwr.innerHTML=“关闭”;
}
选项[0]=pwrtxt;
document.getElementById(“textOutput”).value=选项;
}
功能设置模式(模式){
开关(模式){
“rheat”一案:
选项[1]=“模式=热量”;
打破
案例“rcool”:
选项[1]=“模式=酷”;
打破
案例“rdry”:
选项[1]=“模式=干燥”;
打破
“劳托”案:
选项[1]=“模式=自动”;
打破
“侦察”案:
选项[1]=“模式=经济”;
打破
}
document.getElementById(“textOutput”).value=选项;
}
函数updateTemp(){
var tempString=“temp=”;
选项[2]=tempString.concat(“,temp);
document.getElementById(“textOutput”).value=选项;
}
函数increaseTemp(){
温度+=1;
如果(温度>31){
温度=31;
}
updateTemp();
}
函数decreaseTemp(){