我用Elementor定制代码编写了这个代码

<script>
jQuery(document).ready(function($){
    $('#ship-price').on('click', function(){
            var originalPrice = $('#product-price').val();
            console.log(originalPrice);
            //
            var shipPrice = $('#ship-price').val();          
            console.log(shipPrice);
    });
});
</script>

我正在try Select 使用elementor添加的两个元素,并在其中分配了一个css id.就会出现我无法读取产品价格和发货价格的情况.

我需要实现的是显示一个不同的产品价格是使用woocommerce加载,用户点击后.我如何解决这个问题?

UPDATE个 这是产品价格的html.

<div class="elementor-element elementor-element-2cf415f elementor-widget elementor-widget-heading" data-id="2cf415f" data-element_type="widget" id="product-price" data-widget_type="heading.default">
                <div class="elementor-widget-container">
            <h2 class="elementor-heading-title elementor-size-default"><span class="woocommerce-Price-amount amount"><bdi>499,00&nbsp;<span class="woocommerce-Price-currencySymbol">€</span></bdi></span>*</h2>        </div>
                </div>

这是我需要获取的船价的渲染html

<div id="elementor-tab-content-3201" class="elementor-tab-content elementor-clearfix elementor-active" data-tab="1" role="region" aria-labelledby="elementor-tab-title-3201" style="display: block;"><ul>
<li><a href="#" id="ship-price-a">70€ + IVA</a> ...</li>
<li><a href="#" id="ship-price-b">79€ + IVA</a> ...</li>
</ul></div>

推荐答案

A)由于您的ID不是直接的,所以使用*来匹配它们

B)此外,您的价格都不在输入元素中,它们都在一些HTML元素中,因此使用.text()而不是.val()

c)产品价格在#product-price元素的子元素下,所以你也必须修改代码.

工作代码段:

jQuery(document).ready(function($) {
  $('a[id*=ship-price]').on('click', function(event) {
    event.preventDefault();
    var originalPrice = $('#product-price').find('.woocommerce-Price-amount').text().replace(/[^0-9]/gi, '');
    console.log(originalPrice);

    var shipPrice = $(this).text().replace(/[^0-9]/gi, '');
    console.log(shipPrice);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<div class="elementor-element elementor-element-2cf415f elementor-widget elementor-widget-heading" data-id="2cf415f" data-element_type="widget" id="product-price" data-widget_type="heading.default">
  <div class="elementor-widget-container">
    <h2 class="elementor-heading-title elementor-size-default"><span class="woocommerce-Price-amount amount"><bdi>499,00&nbsp;<span class="woocommerce-Price-currencySymbol">€</span></bdi>
      </span>*</h2>
  </div>
</div>

<div id="elementor-tab-content-3201" class="elementor-tab-content elementor-clearfix elementor-active" data-tab="1" role="region" aria-labelledby="elementor-tab-title-3201" style="display: block;">
  <ul>
    <li><a href="#" id="ship-price-a">70€ + IVA</a> ...</li>
    <li><a href="#" id="ship-price-b">79€ + IVA</a> ...</li>
  </ul>
</div>

Javascript相关问答推荐

有Angular 的material .未应用收件箱中的价值变化

有什么(最佳)方法可以从模块中获取脚本模块的多姆元素吗?

如何访问Json返回的ASP.NET Core 6中的导航图像属性

Vega中的模运算符

在Three JS中看不到补间不透明度更改效果

如何在bslib nav_insert之后更改导航标签的CSS类和样式?

如何在coCos2d-x中更正此错误

ChartJs未呈现

如何使用TypeScrip设置唯一属性?

如何在.NET Core中将chtml文件链接到Java脚本文件?

当我在Reaction中创建一个输入列表时,我的输入行为异常

删除加载页面时不存在的元素(JavaScript)

令牌JWT未过期

FireBase FiRestore安全规则-嵌套对象的MapDiff

用Reaction-RT-Chart创建实时条形图

谷歌饼图3D切片

如何在Web项目中同步语音合成和文本 colored颜色 更改

有角粘桌盒阴影

AstroJS混合模式服务器终结点返回404

Google OAuth 2.0库和跨域开放程序的问题-策略错误