jQuery:如何抓住单选按钮选择?

时间:2010-03-30 20:57:28

标签: jquery radio-button

我需要获取使用jQuery选择的单选按钮选项的值。我使用以下代码,但在Firebug中未定义“str”:

 //handling Feed vs. Ingredient RadioButton
            $('[id$=rdoCheck]').change(function() {
                str = $("input[name='rdoCheck']:checked").val();
                //ingredients
                if (str == "ing")
                { $('[id$="lblDescription"]').text("Ingredient") }
                //finished feed
                else if (str == "ffc")
                { $('[id$="lblDescription"]').text("Finished") }

            });

2 个答案:

答案 0 :(得分:1)

看起来你正在使用ASP.Net。当你查看渲染的html时,你会发现它正在混淆(前缀)name属性,就像它对ID一样。

您还需要在其上使用ends-with selector,如下所示:

var str = $("input[name$=rdoCheck]:checked").val();

答案 1 :(得分:1)

我将类添加到.net生成的元素中,以便我可以使用更简单的jQuery选择器,例如

<asp:textbox id="txtName" CssClass="name"/>

$('input.name').text();

注意:您可以在asp.net 4.0中指定绝对标识符

相关问题