【问题标题】:How can I make a button light up if the right information is entered?如果输入正确的信息,如何使按钮亮起?
【发布时间】:2016-10-15 09:19:02
【问题描述】:

我做了一个排列组合计算器。如果排列 |选择组合并输入值,计算按钮应亮起。如何在不设置间隔的情况下做到这一点。我当前的代码:

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 语句),它就不起作用。

【问题讨论】:

    标签: javascript html css button calculator


    【解决方案1】:

    你需要从输入中调用 keyup 这个函数。

    我做了一个演示:

    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>
    
        

    【讨论】:

      【解决方案2】:

      当您检查值时,您可以在 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";
       }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-06-08
        • 2021-07-29
        • 1970-01-01
        • 1970-01-01
        • 2020-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多