单击按钮启用后,文本字段不会获得焦点

时间:2012-11-26 00:49:31

标签: jquery focus disabled-input

我想在点击按钮后启用它后专注于文本字段但是使用此代码它将无法工作!如果我在启用文本字段时再次单击该按钮,则此代码就像魅力一样。

$('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/

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

提前致谢。

1 个答案:

答案 0 :(得分:1)

您无法对已禁用的元素进行聚焦。首先删除已禁用的属性。

$('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选择器(删除了“)(这就是它对我有用的方式)

相关问题