2016-01-06 51 views
2

我正在研究一个jQuery的移动应用程序,并且遇到将on-change事件绑定到文件输入元素的问题。第一次访问page1.php时,事件正确启动。但是,在访问另一个jQuery网站(page2.php)并返回到page1.php后,事件不会触发(因为我使用chrome的检查工具进行调试,事件甚至没有在第二次访问page1-php时发生) 。当回到页面时,jquery移动事件没有被绑定

我也曾经尝试过不同的页面事件作为http://www.gajotres.net/page-events-order-in-jquery-mobilehttp://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"> 

回来时,第1页为什么不绑定的事件? 我希望得到任何帮助:)

编辑: 一些背景资料:我想加载的QR码的图片。 QR码包含一个serialNumber。取决于我想加载相关页面的serialNumber。应该有可能从这个网站回来扫描一个新的QR码。 Sombody有一个想法以不同的方式解决这个问题?

+0

在页面之间前后移动会使服务器每次都有新的“请求”,它就像从零开始,没有事件被初始化。 –

+0

但它第一次加载页面(就像我重新加载页面1时)。但是当直接从page2返回时,它不起作用。如果它“从零开始”,它应该以与我重新加载页面时相同的方式工作? –

+0

你想实现什么? –

回答

0

我终于找到了问题的原因:

当使page2.php,jQuery的实际上是刚刚起步的第一个数据角色的内容=“页面”分区的链接点击和插入即到当前页面(page2.php)。这是默认的AJAX导航(http://demos.jquerymobile.com/1.4.5/navigation-linking-pages/

因此,page1.php中的脚本(实际上站在html标头中)根本不运行。

有两种解决方案: 如果您想保留AJAX导航,以便获得更好的页面转换,请在data-role =“page”DIV中移动脚本。 我首选其他选项:通过向链接添加data-ajax =“false”来阻止AJAX导航。

<a href="page1.php" data-role="button" class="serial-model-button" data-ajax="false" >