最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Enable an input textbox then focus it - jQuery Mobile - Stack Overflow

programmeradmin4浏览0评论

I have an input textbox which is initially disabled.

<input type="text" name="count" id="count" value="0" disabled="true" data-theme="b" />

When I check a radio button i want to enable and focus the textbox, but when i click the radio button it enables the textbox and focuses some other textbox. I think it does not work because the textbox was initially disabled and you can not focus a disabled input.

$(":input[@name='radio']").live('change', function() { 
  $(":input[@name='count']").removeAttr("disabled").focus();
});

How do I enable the textbox and then focus it?

I have an input textbox which is initially disabled.

<input type="text" name="count" id="count" value="0" disabled="true" data-theme="b" />

When I check a radio button i want to enable and focus the textbox, but when i click the radio button it enables the textbox and focuses some other textbox. I think it does not work because the textbox was initially disabled and you can not focus a disabled input.

$(":input[@name='radio']").live('change', function() { 
  $(":input[@name='count']").removeAttr("disabled").focus();
});

How do I enable the textbox and then focus it?

Share asked Feb 28, 2012 at 12:14 Marius StănescuMarius Stănescu 3,7612 gold badges36 silver badges49 bronze badges 3
  • $(":input[@name='count']").removeAttr("disabled"); $(":input[@name='count']").focus(); – Sam Arul Raj T Commented Feb 28, 2012 at 12:26
  • First remove disabled attr than focus it. like $(":input[@name='count']").removeAttr("disabled") $(":input[@name='radio']").focus(); – Amit Soni Commented Feb 28, 2012 at 12:29
  • nope ... it does not work ... – Marius Stănescu Commented Feb 28, 2012 at 17:37
Add a ment  | 

4 Answers 4

Reset to default 3

It appears it was something wrong with the selector ... although the text input box got enabled, it did not get focused, while I tried:

$(":input[@name='count']").removeAttr("disabled").focus();

But when i tried this, it worked:

$("#count").removeAttr("disabled").focus();

I can not explain why this is happening ...


Edit: It has something to the with the selector, because this works too:

$("input[name='count']").removeAttr("disabled").focus();

but i still can't understand why using the first selector, the textbox gets enabled, but not focused ...

Try below code

$('#count').textinput('enable').focus();

It working fine for me

see

http://jsfiddle/kunalvashist/LFXd2/

You have to do it on pageshow.

  $("#mainPage").on("pageshow", function (e) {
    $('#searchField').focus();
});

on

<body class="ui-mobile-viewport ui-overlay-c">
    <div data-role="page" data-add-back-btn="true" id="mainPage">
        <div data-role="header" data-theme='b'>
           <input type="text" id="searchField" placeholder="Search" />
        </div>
    </div>
</body
发布评论

评论列表(0)

  1. 暂无评论