如何从单选按钮动态获取值?

How do I get values dynamically from radio buttons?

这是页面上任何更改的更改事件。

$(this).change(function(){
    type = $('input[name='+$(this).name+'radioName]:checked', '#myForm').val();
    alert(type);
});

我想获取被点击的单选按钮。所以我可以在点击利润时显示隐藏的输入框。

<TD align=left>
    <input type=radio name="Red" value="l" checked>Loss
    <input type=radio name="Red" value="w">Win
    <input type=radio name="Red" value="p">Profit
    <INPUT value="" name="profitRed" LENGTH="5" hidden>
</TD>
<TD align=left>
    <input type=radio name="Black" value="l" checked>Loss
    <input type=radio name="Black" value="w">Win
    <input type=radio name="Black" value="p">Profit
    <INPUT value="" name="profitBlack" LENGTH="5" hidden>
</TD>

所以无论单选按钮被点击了什么,我都想获取名称并检查值。如果值为"p",那么我要更新对应的文本框

If(type=="p")
{
    $("profit"+$(this).name).show();
}

我是 JQuery 的新手,真的希望有人能帮助我。与必须检查每个名称相比,这会使事情变得容易得多。

非常感谢

$('input[type="radio"]').on('click',function(){
     var value = $(this).val();
     var name = $(this).attr('name');
     if(value == 'p'){
         // add . for class or # for id
         $('#profit'+ name).show();
         // this will show the element with Id='profitBlack'
     }
});

您需要收听输入而不是 $(this)。一旦发生变化,您可以获得名称并检查值是否等于 p.

$('input').change(function(){
    type = $(this).attr('name');
    alert(type);
    if($(this).attr('value')=='p')
        $("profit"+$(this).name).show();
});