获取单选按钮元素的span子项

get span children of a radio button element

本文关键字:span 子项 元素 单选按钮 获取      更新时间:2023-09-26

我有一排单选按钮,用于支付运费。如果用户单击按钮,我需要收集单选按钮元素使用的div中包含的两个跨度(价格和成本)。所有的单选按钮都共享一个名为"所有项目"的类。这是html:

<li class="all-items">
<input name="shipping_method" type="radio" value="usps_1" id="s_method_usps_1" checked="checked" class="radio">
<label for="s_method_usps_1" style="display:block; overflow:hidden;">
<span class="price">$30.20</span>
<span class="cost">Priority Mail </span>
</label>
</li>

这是我正在使用的jQuery。

         jQuery("input:radio[name=shipping_method]").click(function() {
         var shipping = jQuery(this).val();
         console.log(shipping);
         console.log(jQuery(this).find('span'));
         });

SPAN不是无线电元素的子元素。我建议你找到相关的LI家长,然后瞄准相关的SPAN,例如:

jQuery(this).closest('.all-items').find('.price, .cost') // add specific SPAN classes if needed

我个人更喜欢它而不是jQuery(this).closest('.all-items').find('span'),因为它清楚地告诉了你正在寻找哪些特定的SPAN。