访问在html标签之间滑动的单选按钮的值

Accessing the value of a radio button sliding among html tags

本文关键字:单选按钮 之间 html 标签 访问      更新时间:2023-09-26

这段代码在我的页面中出现了几次:

<div>
   <input type="hidden" name="product" id="productF3" value="7">
   <div>
    <input type="radio" name="size" id="size_s" value="3"> small size
   </div>
   <div>
   <input type="radio" name="size" id="size_n" value="4"> regular size
   </div>
</div>

我在从productF3元素开始获取所选值时遇到了问题。现在我用这段代码来解决我的问题:

if($("#productF3").next().children("input[@name=size]:checked").val() != undefined) do something
else if($("#productF3").next().next().children("input[@name=size]:checked").val() != undefined) do something
else alert("Select a size");

但是我想要一个更健壮的解决方案,有什么建议吗?

谢谢!

先将$("productF3")改为$("#productF3")。其次,为什么不用

$("#productF3").parent().find("input[type='radio'][name='size']:checked").val()

获取选定的单选值?