【发布时间】:2016-04-10 13:50:01
【问题描述】:
我正在开发一个 jquery 移动应用程序,但在将 on-change 事件绑定到文件输入元素时遇到了问题。我第一次访问 page1.php 时,该事件会正确触发。但是在访问了另一个 jquery 站点(page2.php)并且返回到 page1.php 之后,事件并没有触发(当我使用 chrome 的检查工具进行调试时,我第二次访问 page1-php 时甚至没有绑定该事件) .
我还尝试了不同的页面事件来绑定事件,如http://www.gajotres.net/page-events-order-in-jquery-mobile 中所述 和http://www.gajotres.net/prevent-jquery-multiple-event-triggering/
这是page1.php的代码:
$(document).on('pagebeforeshow', '#serialSelect', function(){
$("#qrImageSelect").off('change', '#qrImageFile').on('change','#qrImageFile' , function(){
var selectedFiles = $('#qrImageFile')[0].files;
serialNumber = readSerialNumber(selectedFiles);
window.location.href = "page2.php?sn="+serialNumber;
});
});
<link href="http://code.jquery.com/mobile/1.4.5/jquery.mobile.structure-1.4.5.min.css" rel="stylesheet"/>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<form id="qrImageSelect" class="ui-btn ui-body ui-body-a ui-corner-all ui-icon-plus ui-btn-icon-left fileinput-button" accept-charset="UTF-8">
<span>QR code</span>
<input type="file" name="qrImageFile" data-role="none" id="qrImageFile" />
</form>
page2.php(相同的jquery版本)
<a href="page1.php" data-role="button" class="serial-model-button">
为什么返回page1时没有绑定事件? 我会很感激任何帮助:)
编辑: 一些背景信息:我想加载一张二维码的图片。二维码包含一个序列号。根据我想加载相关页面的序列号。应该可以从该站点返回扫描新的二维码。有人有想法以不同的方式解决这个问题吗?
【问题讨论】:
-
在页面之间来回切换每次都会给服务器带来新的
requests,就像从零开始,没有初始化任何事件。 -
但它在我第一次加载页面时工作(就像我重新加载 page1 时一样)。但是当直接从page2返回时它不起作用。如果它“从零开始”,它应该以与我重新加载页面时相同的方式工作?
-
你想达到什么目的?
-
感谢您到目前为止的帮助。我想加载一张 QR 码的图片。二维码包含一个序列号。根据我想加载相关页面的序列号。应该可以从该站点返回扫描新的二维码。希望这是可以理解的......
标签: javascript jquery html jquery-mobile