html代码:
<div align="center">
<input type="text" id="txt" style="width: 300px; margin-top:100px">
<button id="btn" style="margin-top: 30px;">确认</button>
</div>
js代码:
<script type="text/javascript">
//button默认不可用
$("#btn").prop("disabled",true);
$("#txt").bind("input propertychange",function () {
if($("#txt").val().length>0){
$("#btn").prop("disabled",false);
}
else
$("#btn").prop("disabled",true);
});
</script>
记得引用jq:
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
补充:
对于HTML元素本身就带有的固有属性,在处理时,使用prop方法。
对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法。