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

查看:17
本文介绍了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天全站免登陆