简体   繁体   English

jQuery .live('click',function(){}); 不适用于iPad

[英]jQuery .live('click', function() {}); doesn't work for iPad

I have some jQuery functions which update some numbers when new values are selected in a <SELECT> field. 我有一些jQuery函数,当在<SELECT>字段中选择新值时,这些函数会更新一些数字。 The <SELECT> items are added dynamically by the user, so these functions use the .live() function. <SELECT>项目是由用户动态添加的,因此这些函数使用.live()函数。 Everything goes as planned in Mozilla, Chrome, and IE8, but when I try to access the page on my iPad the numbers are no longer being updated accordingly. 在Mozilla,Chrome和IE8中,一切按计划进行,但是当我尝试访问iPad上的页面时,数字不再相应地更新。 I have to click the <SELECT> field a second time for the numbers to update. 我必须点击 <SELECT>字段的号码更新的时候 Here is one of my functions: 这是我的功能之一:

    // Update subtotal when .months is changed
    $(".months").live('click',function(){

        var pid = this.id.replace("months", "");
        var price = document.getElementById('row' + pid).className;
        updateSubtotal(pid, price);

    });

All of the functions are contained within the document.ready() block. 所有功能都包含在document.ready()块中。 I've tried 'click change' and just 'change' as well, but they have issues in some other browsers. 我尝试过“点击更改”,也尝试过“更改”,但是它们在某些其他浏览器中存在问题。

Thanks for any and all help! 感谢您提供的所有帮助!

iPad and select elements don't play very nice. iPad和某些选择元素玩起来不太好。 You will need to bind to blur if you need the selected value to do anything. 如果您需要选定的值执行任何操作,则需要绑定以模糊

Try something like this ( .months should be your select element ): 尝试这样的事情( .months应该是您的select元素):

// Update subtotal when .months is changed
$(".months").on('blur', function(){

    var pid = this.id.replace("months", "");
    var price = document.getElementById('row' + pid).className;
    updateSubtotal(pid, price);

   });

EDIT 编辑
Try this: 尝试这个:

// Update subtotal when .months is changed
$(document).on('blur|click', ".months", function(){
    var pid = this.id.replace("months", "");
    var price = document.getElementById('row' + pid).className;
    updateSubtotal(pid, price);
    });

The nice thing about on() is that you can attach it to an element and delegate from there. on()的好处是,您可以将其附加到元素上并从那里进行委托。 Since your $(document) will always be there it can also catch elements you add dynamically to the page and which are not there from the beginning. 由于您的$(document)将始终存在,因此它还可以捕获您动态添加到页面中且从一开始就不存在的元素。 Since your original code used live I expect the element to by loaded or generated dynamically. 由于您的原始代码实时使用,所以我希望元素可以动态加载或生成。 In that case in order to bind an event to it using on() you have to bind to the document and delegate to whatever .months is coming. 在这种情况下,为了使用on()将事件绑定到事件,您必须绑定到文档委托任何即将到来的.months

如果您将一个空的onclick =“”添加到div(或span或其他任何东西),它将起作用....我不知道为什么:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM