jqueryMobile设置了弹出窗口中的输入字段

时间:2013-05-01 10:40:01

标签: jquery jquery-mobile focus jquery-mobile-popup

在我的jquery移动应用程序中,我想在打开弹出窗口后自动将焦点设置在输入字段上。输入字段位于弹出窗口中,如下所示。焦点在开始时正确设置,但在弹出窗口完全降低后丢失。

    <!-- 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/

感谢您的帮助

善意的反叛

1 个答案:

答案 0 :(得分: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();
        }
    });
});