首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >点击事件不适用于按钮,由JS生成

点击事件不适用于按钮,由JS生成
EN

Stack Overflow用户
提问于 2019-05-20 01:25:04
回答 1查看 21关注 0票数 1

当我选择"Keyboard layout"时,它会使用js生成按钮,但单击事件不能使用动态生成的按钮。我认为,这是因为当文档准备就绪时,元素".prices-tier“不存在。只有当我选择布局时,它才会生成。

我的部分代码:

    require(['jquery'], function(){
    jQuery(document).ready(function(){
        const currencySymbol = jQuery('.price').text()[0];
        var standartPrice = Number(jQuery('.price-wrapper').attr('data-price-amount'));



        jQuery('.prices-tier').on('click','.btn', function () {
            var quantity = Number(jQuery(this).attr("data-qty"));
            var price = jQuery(this).attr("data-amount");
            jQuery('.qty-default').val(quantity);
            jQuery('#product-price-'+"<?php echo $_product->getId();?>"+' .price').html(currencySymbol+price);
            // jQuery('.product-info-main>div>.price-box>.price-container>.price-wrapper>.price').html(currencySymbol+price);
            jQuery('.qty-default').trigger('input');
        }
        );

生成的html元素:

<div class="prices-tier items w-75 btn-group">

        <button type="button" class="btn btn-light border border-bottom-0 border-top-0 bg-primary" data-qty="25" data-amount="27.21" onclick="">

        4%</button>

        <button type="button" class="btn btn-light border border-bottom-0 border-top-0 bg-primary" data-qty="50" data-amount="26.5" onclick="">

        6%</button>
</div>
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56210300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档