代码之家  ›  专栏  ›  技术社区  ›  Michael

如果div不为空,如何启用按钮?

  •  0
  • Michael  · 技术社区  · 6 年前

    我有这个div和靠近div的按钮:

    <div id="tempData"></div>
    <input type="button" text="status" disabled>
    

    在某个时候,我以编程方式向div添加元素,当div不为空时,必须启用它。 如何使按钮在div不为空时启用,在div为空时禁用?

    2 回复  |  直到 6 年前
        1
  •  3
  •   clearshot66    6 年前

    纯javascript:

    当触发动作发生时,首先检查空,不管是什么:

    function whatevertriggeredthis(){
        if(document.getElementById("tempData").innerHTML === ""){
    
           // Give the button an ID then enable it
           document.getElementById("status_button").disabled = false;
    
        }
    }
    

    JQuery版本:

    $("#tempData").on("change",function(){
       if($(this).text() !== ""){
          $("button").prop("disabled",false);
       }
    });
    
        2
  •  1
  •   Amit Bhoyar    6 年前

    function yourFunctionToUpdateDiv(){
    
      //YOUR CODE TO ADD/REMOVE DIV CONTENT
      //...
      //...
      
      
      if ($('#tempData').html()){ 
        $("input").prop('disabled', false);
      }
      else{
        $("input").prop('disabled', true);
      }
    }