douxiao0400 2015-11-13 22:51
浏览 138

Ajax生成的内容需要双击才能触发单击jQuery

On my view (ajax generated):

<a data-name="notes" href="#" data-type="text" data-pk="{!! $v->id !!}" class="vpUpdate">{{ $v->notes}}</a>

On my jQuery:

$(document).on('click', '.vpUpdate', function (e) {
    $.fn.editable.defaults.mode = 'popup';
    $('.vpUpdate').editable({
        type: 'text',
        url:'edit-notes',   
        title: 'Edit Notes',
        placement: 'top', 
        send:'always',
        ajaxOptions: {
            dataType: 'json'
        }
    });
});

Now this works an all but only if I click it twice then the jQuery triggers.

I did not have this issue before with using only:

$(document).ready(function(){

However this method won't work if the content was ajax generated.

Also, let's say I have multiple links on the page that uses the same .vpUpdate class as the trigger, if I double click on any of them once, any future clicks will only require a single click from then on. If I refresh the page this whole cycle happens again.

Now I was just wondering how I can get it to trigger the jQuery immediately on first click instead of double clicking.

Thanks.

  • 写回答

1条回答 默认 最新

  • doujianchao7446 2015-11-13 23:11
    关注

    You have to wrap the assignment of eventhandlers inside a $(document).ready function or a $(window).load function. You are attaching the click event to the document object before the document object actually exists.

    $(document).ready(function(){
        $(document).on('click', '.vpUpdate', function (e) {
            $.fn.editable.defaults.mode = 'popup';
            $('.vpUpdate').editable({
                type: 'text',
                url:'edit-notes',   
                title: 'Edit Notes',
                placement: 'top', 
                send:'always',
                ajaxOptions: {
                    dataType: 'json'
                }
            });
        });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line