【问题标题】:Text field does not take focus after being enabled on click on a button单击按钮启用后文本字段不聚焦
【发布时间】:2012-11-26 00:49:31
【问题描述】:

我想在单击按钮后启用文本字段后将注意力集中在文本字段上,但是使用此代码将无法正常工作!如果我在启用文本字段时再次单击该按钮,则此代码就像一个魅力。

$('body').on('click', '#button', function() {
    if($('input[name="textfield-contact-name"]').val() == '') {
        $('input[name="textfield-contact-name"]').focus();
    }

    $('input').attr('disabled', false);
});

我已经测试过在文本字段中使用id="field",然后在jQuery 中使用#field,但同样的问题仍然存在!演示:http://jsfiddle.net/edgren/Wj5Cq/

为什么启用文本字段后,文本区域不聚焦?

提前致谢。

【问题讨论】:

    标签: jquery focus disabled-input


    【解决方案1】:

    您无法聚焦已禁用的元素。先移除 disabled 属性。

    $('body').on('click', '#button', function() {
    
      $('input').removeAttr("disabled");
    
      if($('input[name=textfield-contact-name]').val() == '') {
        $('input[name=textfield-contact-name]').focus();
      }
    });
    

    ​ 另外,我认为禁用的标记应该是:

    <input type="text" name="textfield-contact-name" disabled="disabled">
    

    我还更改了您的 jQuery 选择器(删除了 " )(对我来说就是这样)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-23
      • 1970-01-01
      • 1970-01-01
      • 2020-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多