如果输入了正确的信息,我怎样才能使按钮亮起?
How can I make a button light up if the right information is entered?
我做了一个排列组合计算器。如果排列 |选择组合并输入值后,计算按钮应亮起。如果不设置间隔,我怎么能做到这一点。我当前的代码:
function checkCalc(){
if(((permutation==true)||(combination==true))&&(document.getElementById('r').value.length>0)&&(document.getElementById('n').value.length>0)){
calculate.style.backgroundColor="#ccccff";
calculate.style.boxShadow="10px 10px 5px gray";
}
else{
calculate.style.backgroundColor="gray";
}
}
setInterval(checkCalc, 50);
如果我删除函数(只留下 if 语句),它就不起作用。
当您无论如何都要检查该值时,您可以在 r
文本字段上设置一个事件侦听器(我假设它是一个文本字段?),一旦您输入内容就会触发:
document.getElementById('r').onkeydown=function(){
if(((permutation==true)||(combination==true))&&(document.getElementById('r').value.length>0)&&(document.getElementById('n').value.length>0)){
calculate.style.backgroundColor="#ccccff";
calculate.style.boxShadow="10px 10px 5px gray";
}
else{
calculate.style.backgroundColor="gray";
}
}
您需要在输入的 keyup 上调用此函数。
我做了个demo:
var permutation=true;
var combination=true;
var calculate= document.getElementById('calculate')
function checkCalc(){
if(((permutation==true)||(combination==true))&&(document.getElementById('r').value.length>0)&&(document.getElementById('n').value.length>0)){
calculate.style.backgroundColor="#ccccff";
calculate.style.boxShadow="10px 10px 5px gray";
}else{
calculate.style.backgroundColor="gray";
}
}
<input type ="text" id ='r' onkeyup="checkCalc()">
<input type ="text" id ='n' onkeyup="checkCalc()">
<button id="calculate">Light</button>
我做了一个排列组合计算器。如果排列 |选择组合并输入值后,计算按钮应亮起。如果不设置间隔,我怎么能做到这一点。我当前的代码:
function checkCalc(){
if(((permutation==true)||(combination==true))&&(document.getElementById('r').value.length>0)&&(document.getElementById('n').value.length>0)){
calculate.style.backgroundColor="#ccccff";
calculate.style.boxShadow="10px 10px 5px gray";
}
else{
calculate.style.backgroundColor="gray";
}
}
setInterval(checkCalc, 50);
如果我删除函数(只留下 if 语句),它就不起作用。
当您无论如何都要检查该值时,您可以在 r
文本字段上设置一个事件侦听器(我假设它是一个文本字段?),一旦您输入内容就会触发:
document.getElementById('r').onkeydown=function(){
if(((permutation==true)||(combination==true))&&(document.getElementById('r').value.length>0)&&(document.getElementById('n').value.length>0)){
calculate.style.backgroundColor="#ccccff";
calculate.style.boxShadow="10px 10px 5px gray";
}
else{
calculate.style.backgroundColor="gray";
}
}
您需要在输入的 keyup 上调用此函数。
我做了个demo:
var permutation=true;
var combination=true;
var calculate= document.getElementById('calculate')
function checkCalc(){
if(((permutation==true)||(combination==true))&&(document.getElementById('r').value.length>0)&&(document.getElementById('n').value.length>0)){
calculate.style.backgroundColor="#ccccff";
calculate.style.boxShadow="10px 10px 5px gray";
}else{
calculate.style.backgroundColor="gray";
}
}
<input type ="text" id ='r' onkeyup="checkCalc()">
<input type ="text" id ='n' onkeyup="checkCalc()">
<button id="calculate">Light</button>