使用jquery从单选按钮获取span价格

Get span price from radio button using jquery

本文关键字:span 价格 获取 单选按钮 jquery 使用      更新时间:2023-09-26

span的类别为.amount,现在我想从span

获取价格

 $('.dimension-layer-dimension').click(function() {
   // I am trying with this javascript
   var price4 = $(this).find('radio:checked').data('price')
   $('span.amount').text(price);
   alert(price4);
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="radio">
  <input class="tmcp-field dimension-layer-dimension tm-epo-field tmcp-radio" name="tmcp_radio_9" data-price="" type="radio">
  <label for="tmcp_choice_9_0_17">72*30</label>
  <span class="price tc-price  hidden">
       <span class="amount">1000</span>
  </span>
</li>

您应该使用遍历到共同父元素li.radio,然后使用.find()来获取元素。

使用.text()获取文本并设置.text(newValue)

 $('.dimension-layer-dimension').click(function() {
    var spanEl = $(this).closest('li.radio').find('span.amount');
    var price = spanEl.text(); //Get text
 });