4

In my jquery mobile application I would like to automatical set the focus on an input field after a popup is opened. The input field is in the popup as below. The focus is correctly set at the beginning but lost after the popup is fully lowded.

    <!-- Link--><a onclick="$('#popup_input').focus()" href="#popup" data-iconpos="top" data-rel="popup" data-position-to="window" data-role="button" data-inline="false" data-transition="pop" data-icon="plus" data-theme="b">open</a>
<div data-role="popup" id="popup" data-overlay-theme="a" data-theme="c" data-dismissible="false" style="max-width:400px;" class="ui-corner-all">
            <div data-role="header" data-theme="a" class="ui-corner-top">
                 <h1>Popup</h1>

            </div>
            <div data-role="content" data-theme="d" class="ui-corner-bottom ui-content">
                 <h3 class="ui-title">Focused Field</h3>

                <p>
                    <input type="text" id="popup_input" />
                </p> <a href="#" data-role="button"data-rel="back">Close</a>

            </div>
        </div> 

http://jsfiddle.net/kx7Gz/

Thanks for your help

kind reagrds

4

1 回答 1

8

工作示例:http: //jsfiddle.net/Gajotres/BknCc/

您需要使用一点 javascript 来完成这项工作。不要使用内联 javascript 将焦点设置在字段上。而是等待弹出窗口完全创建并显示,然后再执行任何操作。

HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>jQM Complex Demo</title>
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
        <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>    
    </head>
    <body>
        <div data-role="page" id="index">
            <div data-theme="b" data-role="header">
                <h1>Index page</h1>
            </div>

            <div data-role="content"> 
                <a href="#popup" data-iconpos="top" data-rel="popup" data-position-to="window" data-role="button" data-inline="false" data-transition="pop" data-icon="plus" data-theme="b">open</a>

                <div data-role="popup" id="popup" data-overlay-theme="a" data-theme="c" data-dismissible="false" style="max-width:400px;" class="ui-corner-all">
                    <div data-role="header" data-theme="a" class="ui-corner-top">
                        <h1>Popup</h1>

                    </div>
                    <div data-role="content" data-theme="d" class="ui-corner-bottom ui-content">
                        <h3 class="ui-title">Focused Field</h3>

                        <p>
                            <input type="text" id="popup_input" />
                        </p> <a href="#" data-role="button"data-rel="back">Close</a>

                    </div>
                </div>
            </div>
        </div>    
    </body>
</html>   

Javascript:

$(document).on('pagebeforeshow', '#index', function(){ 
    $( "#popup" ).popup({
        afteropen: function( event, ui ) {
            $('#popup_input').focus();
        }
    });
});    
于 2013-05-01T11:34:14.310 回答