jQuery mobile .click在新页面访问上触发多次 [英] Jquery mobile .click firing multiple times on new page visit

查看:65
本文介绍了jQuery mobile .click在新页面访问上触发多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用Jquery mobile取得了很大的成功,除了一个问题. 现在,无论用户显示什么内容,每次用户浏览动态内容页面时,始终在底部都有一个按钮,当单击该按钮时,会将内容通过电子邮件发送到指定的地址.效果很好. 现在,第一次加载页面时,单击会触发一次事件.在第二次访问中,它被发射了两次,第三次被发射了3次,等等.我已经搜寻了网络并实现了我可能遇到的所有修复程序,例如使用"pagecreate"而不是"pageinit",绑定,解除绑定,删除div,关闭DOM中的缓存,但是没有任何效果.我获得的唯一成功是在单击上使用了.one(),但是如果再次单击,则需要将其触发.这是结构.我必须.js个文件与此一起加载

So I am using Jquery mobile with great success except for one issue. Now every time a dynamic content page is navigated to by a user no matter what content displays, there is always a button at the bottom that when clicked emails the content to the address specified; works great. Now the first time a page is loaded, the click fires the event once. On the second visit its fired twice, third 3 times, etc. you get the point. I've scoured the net and implement every fix I could come across, such as using "pagecreate" instead of "pageinit", binding, unbinding, removing the div, turning off caching in the DOM but nothing works. The only success I've had is using .one() on the click but it needs to be fired if clicked again. Here is the structure. I have to .js files that load each with this to start

$( "#page" ).live( "pageinit", function() { 

现在,我在一个文件中具有电子邮件功能和其他内容,但是它使分隔它们变得更加容易,而且我听说这没关系.现在,这是pageinit

Now I had the email function and other stuff in one file, but it makes it easier to separate them and I heard it does not matter. Now, here is the email function called in the pageinit

$('.eb').live('click', function() {
        var id = $('.eb').attr('id');
        var to = $('#recipient').val();
        var message = $('#email').html();

        var atpos = to.indexOf("@");
        var dotpos = to.lastIndexOf(".");
        if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= to.length) {
            alert('Please enter a valid email address!');
            return false;
        } 

        if(to.length == 0) {
            alert('Please enter a valid email address!');
            return false;
        } 

        $.mobile.showPageLoadingMsg();

        $.post('./services/service.email.php', { id: id, to: to, message: message}, function(data) {
            if(data.success == true) {
                $.mobile.hidePageLoadingMsg();
                alert('Your recipe has been sent!');
                $('#recipient').val('');
                return true;
            } 

            if(data.success == false) {
                if(data.fail == 1) {
                    alert('An error has occured sending your recipe. Try again soon!');
                    return false;
                }

                if(data.fail == 2) {
                    alert('Please enter a valid email address!');
                }
            }
        }, 'json');
            return false;
    }); 

除了在每个新页面上逐步触发.click之外,其他所有内容都可以正常工作. 谁能引导我朝正确的方向前进?

Everything works flawlessly except the incremental firing of the .click on each new page. Can anyone lead me in the right direction?

推荐答案

我通过意识到div本身就是问题#page解决了这一问题. 该页面加载了动态内容,但id从未加载过,因此沿行将其缓存在某个位置,并在每次新访问中被递增触发. 我刚刚添加了一些PHP代码,以在每次加载页面时创建一个唯一的div ID.

I solved this by realizing the issue was with the div itself, #page. This page was loaded with dynamic content but the id never did, so somewhere along the line it cached somewhere and was fired incrementally on each new visit by one. I just added some PHP code to create a unique div id on each page load.

<?php $id = uniqid() ?>
<div id="<?php echo $id; ?>">CONTENT</div>

这解决了问题,并且无论页面重新加载的方式如何,它都只会触发一次.

This solved the problem and it fires only once no matter how may page reloads there are.

这篇关于jQuery mobile .click在新页面访问上触发多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆