jq实现文本框有值时button可用,无值时不可用

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方法。

_AD
发布了9 篇原创文章 · 获赞 0 · 访问量 412

猜你喜欢

转载自blog.csdn.net/weixin_43553153/article/details/103700372