【发布时间】:2013-05-23 09:48:56
【问题描述】:
我有两个 html 页面,每个页面都包含 JQM 页面。如果我从 Page1 移动到 Page2(通过使用 $.mobile.changePage 和 reloadPage=true 单击示例中的按钮)然后返回到 Page1 然后分配任何值到 txt1 文本框(在 pageshow 事件内)不起作用 - 文本框保持空白。但是在 第二次旅行(即 Page1->Page2->Page1 使用按钮单击)它可以工作。代码示例在这里:
Page1.htm
<!DOCTYPE>
<html>
<head>
<title>Page-1</title>
<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/jquery-1.9.1.min.js"></script>
<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="page_1" data-theme="a" data-url="/JqmSimpleApp/Page1.htm">
<div data-role="header" class="header_bar">
Page-1 Header
</div>
<div data-role="content" class="investor-centre">
Page-1 CONTENT
<br/>
<a id="btnMoveToPage2" href="#" data-role="button" data-inline="true" data-theme="b">Move to Page-2</a>
<input type="text" id="txt1"/>
</div>
<div data-role="footer" data-theme="c" data-position="fixed">
Page-1 Footer
</div>
<!--################### Page specific Script Section ############################# -->
<!--NOTE: The reason it's been put under the data-role="page" section is that when a page is been loaded using Ajax call JQM only loads the content within data-role="page" section. -->
<script type="text/javascript">
$('body').off('pageinit', "#page_1").on('pageinit', "#page_1", function () {
alert('Page-1 INIT');
$("body").off("click", "#btnMoveToPage2").on("click", "#btnMoveToPage2", function () {
$.mobile.changePage("Page2.htm", { reloadPage: true });
});
$("body").off("click", "#btnShow").on("click", "#btnShow", function () {
var myDate = new Date();
$("#txt1").val(myDate);
alert('Clicked');
});
});
$('body').off('pageshow', "#page_1").on('pageshow', "#page_1", function () {
alert('Page-1 SHOW');
var myDate = new Date();
$("#txt1").val(myDate); //*** PROBLEM HERE - Doesn't show any value in the 1st round trip.****\\\
alert('txt1 value: ' + $("#txt1").val());// STRANGE!! When I read from txt1 text box it ALWAYS shows value
});
</script>
</div>
</body>
</html>
Page2.htm
<!DOCTYPE>
<html>
<head>
<title>Page-2</title>
<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/jquery-1.9.1.min.js"></script>
<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="page_2" data-theme="c" data-url="/JqmSimpleApp/Page2.htm">
<div data-role="header" class="header_bar">
Page-2 Header
</div>
<div data-role="content" class="investor-centre">
Page-2 CONTENT
<br/>
<a id="btnMoveToPage1" href="#" data-role="button" data-inline="true" data-theme="b">Move to Page-1</a>
<input type="text" id="txt2"/>
</div>
<div data-role="footer" data-theme="c" data-position="fixed">
Page-2 Footer
</div>
<!--################### Page specific Script Section ############################# -->
<!--NOTE: The reason it's been put under the data-role="page" section is that when a page is been loaded using Ajax call JQM only loads the content within data-role="page" section. -->
<script type="text/javascript">
$('body').off('pageinit', "#page_2").on('pageinit', "#page_2", function () {
alert('Page-2 INIT');
$("body").off("click", "#btnMoveToPage1").on("click", "#btnMoveToPage1", function () {
$.mobile.changePage("Page1.htm", { reloadPage: true });
});
});
$('body').off('pageshow', "#page_2").on('pageshow', "#page_2", function () {
alert('Page-2 SHOW');
var myDate = new Date();
$("#txt2").val(myDate);
});
</script>
</div>
</body>
</html>
请注意,我试图在 Page1 的 pageshow 事件中使用 $("#txt1").val(myDate) 为输入元素 txt1 赋值。文本框保持空白,但是,如果我从该文本框中读取值并显示在警报中,我可以在警报框中看到值。我是否必须以某种方式刷新 UI,还是我使用错误的事件来访问该元素?
进一步调查:
稍后,我尝试使用一个附加页面 (index.htm),从其中移动到 Page1,然后是 Page2,然后是往返。所以 index.htm->Page1.htm->Page2.htm->Page1.htm 这一次没有问题。因此,这意味着通过 HTTP 加载的第一页存在问题。随着使用 AJAX 加载后续页面($.mobile.changePage 和 reloadPage=true),它们可以正常工作。我不得不说这是 JQM 中的一个错误。有什么解决办法吗?
这是我的索引页:
index.htm
<!DOCTYPE>
<html>
<head>
<title>Page-1</title>
<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/jquery-1.9.1.min.js"></script>
<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="page_1" data-theme="a" data-url="/JqmSimpleApp/Page1.htm">
<div data-role="header" class="header_bar">
Page-1 Header
</div>
<div data-role="content" class="investor-centre">
Page-1 CONTENT
<br/>
<a id="btnMoveToPage2" href="#" data-role="button" data-inline="true" data-theme="b">Move to Page-2</a>
<input type="text" id="txt1"/>
<a id="btnShow" href="#" data-role="button" data-inline="true" data-theme="b">Show</a>
</div>
<div data-role="footer" data-theme="c" data-position="fixed">
Page-1 Footer
</div>
<!--################### Page specific Script Section ############################# -->
<!--NOTE: The reason it's been put under the data-role="page" section is that when a page is been loaded using Ajax call JQM only loads the content within data-role="page" section. -->
<script type="text/javascript">
$('body').off('pageinit', "#page_1").on('pageinit', "#page_1", function () {
//alert('Page-1 INIT');
$("body").off("click", "#btnMoveToPage2").on("click", "#btnMoveToPage2", function () {
$.mobile.changePage("Page2.htm", { reloadPage: true });
});
$("body").off("click", "#btnShow").on("click", "#btnShow", function () {
var myDate = new Date();
$("#txt1").val(myDate);
alert('Clicked');
});
});
$(document).off('pageshow', '#page_1').on('pageshow', "#page_1", function (event) {
//alert('Page-1 SHOW - ' + $("#txt1").length);
$("#txt1").val('');
var myDate = new Date();
$("#txt1").val(myDate); //*** PROBLEM HERE - Doesn't show any value in the 1st round trip.****\\\
alert('txt1 value: ' + $("#txt1").val()); // STRANGE!! When I read from txt1 text box it ALWAYS shows value
});
</script>
</div>
</body>
</html>
【问题讨论】:
-
为什么将
click事件包装在pageinit中?在pageinit之外进行click事件,然后重试。pageinit只触发一次,一旦页面插入 DOM。 -
您好奥马尔,感谢您的回复。点击事件对我来说不是问题。它按预期触发。我想在每次访问该页面时显示当前日期时间。这就是为什么我使用 pageshow 事件,该事件假设在我每次访问页面时都会执行,但它在第一次往返时不会执行,而是在页面第一次加载和第二次往返时执行。
-
使用唯一 ID
#txt1和#txt2。另外,$(document).on('pageshow', '#page_1', function () { your code }); -
我已经在使用唯一 ID(请参见示例)并尝试过 $(document).on('pageshow'... 但没有运气 :-(
标签: jquery jquery-mobile