根据另一个输入值隐藏输入

Hide input according to another input value

我有这个简单的表格:

<form align="center" onload="chk()" name="form" action="" method="post" id="form">  
  <input type="text" name="quantt" id="quantt" value="">    
  <input type="text" name="avail" id="avail" value="">  
</form>

"avail" 输入是从 php 脚本自动填充的。我的问题是,如果 "avail" 值为空 在加载表单 时如何隐藏或禁用 [​​=23=] 输入?

我试过这个功能,但没用:-(

function chk() {
    var ava = document.form.avail;
    if (ava.value == "") {
        document.getElementById("quantt").type = "hidden";
    } else {
        document.getElementById("quantt").type = "text";
    }
}

非常感谢您的帮助。

chk() 函数似乎工作正常。尝试使用 window.onload 调用它,例如:

window.onload = function() {
    chk();
};

有关详细信息,请参阅 this 问题。

Use below function

<script>
    function chk() 
    {
        if ( $("#avail").val() == "" ) {
            $("#quantt").attr("hide", true)
        } else {
            $("#quantt").attr("hide", false)
        }
    }
</script>

使用 Element.querySelector() 您可以在 body 加载时调用函数 chk

document.querySelector('body').onload = function chk() {
  var qte = document.getElementById('quantt')
  document.getElementById('avail').type = (qte.value === '') ? 'hidden' : 'text';
};
<form align="center" name="form" action="" method="post" id="form">  
  <input type="hidden" name="quantt" id="quantt" value="">    
  <input type="text" name="avail" id="avail" value="">    
</form>