jQuery脚本,用于检查输入是否有任何值.IE 和 Chrome 中的问题

jQuery script for checking if input has any value. Issue in IE and Chrome

本文关键字:IE Chrome 问题 任何值 脚本 用于 检查 是否 输入 jQuery      更新时间:2023-09-26

我有检查输入是否为空的脚本。它在 Firefox 中有效,但是如果我切换到 Chrome 或 IE,即使我提交一个空字符串,按钮也会启用。有人知道这个问题的任何解决方案吗?

 $(document).ready(function(){
 $('.comment_button').attr('disabled',true);
 $('.comment_input').keyup(function(){
    if ($.trim($(this).val()).length != 0)  {
        $('.comment_button').attr('disabled', false);
    }
    else
    {
        $('.comment_button').attr('disabled', true);        
    }
 })
 });

代码中有一个拼写错误'选择器后丢失

使用$('.comment_input')而不是$('.comment_input)

排队

$('.comment_input).keyup(function(){

编辑:使用 .prop() 而不是 .attr()

工作代码 JS:

 $('.comment_button').prop('disabled', true);
 $('.comment_input').keyup(function () {
      $('.comment_button ').prop('disabled', $.trim($(this).val()).length == 0);
 });

.HTML:

<input type="text" class="comment_input" name="somethng"/>
<button class="comment_button">something</button>

小提琴演示