在jquery中访问具有相同名称的单选按钮

时间:2014-09-24 11:59:29

标签: jquery html asp.net-mvc-4

我在视图中有以下代码。它有两个同名的单选按钮。它在控制器中工作正常,我可以使用单选按钮名称访问它,从而为我提供所选的是或否值 所以如果用户选择"否"在表单中,我将以[" rbc"]的形式访问它,它给了我" No"

的价值

虽然在jquery中不是这种情况,但我无法访问此单选按钮以检查用户是否选择了是或否。请建议我如何在jquery中实现它。

我正在为每个字段分配一个类"验证"如果必填字段是"是"否则是空的。为了从jquery(客户端)验证控件

当前jquery代码迭代所有单选按钮。但我需要它只迭代一次具有相同名称的单选按钮一次。

控制器

        signup.Controls = new List<MembershipControls>();

        foreach(var controlItem in _membershipcontrols.Table.ToList())
        {
            signup.Controls.Add(new MembershipControls()
            {
                Caption = controlItem.Caption,
                Content = controlItem.Content,
                ControlType = controlItem.ControlType,
                GroupTitle = controlItem.GroupTitle,
                Mandatory = controlItem.Mandatory == "Yes" ? "validate" : "",
                Name = controlItem.Name,
                UserId = controlItem.UserId
            }); //modify mandatory field to assign class "validate" and empty to not validate
        }




        signup.Groups = new List<Groups>();
        signup.Groups = _groups.Table.ToList();

        return View("Signup", signup);

查看

          @foreach (var groups in Model.Groups)
            {
                    <label style="font-weight:bold">@groups.GroupTitle</label>

                    <div style=" border: 1px solid #CCCCCC;padding:5px">

                    @foreach (var row in Model.Controls.Where(r => r.GroupTitle == groups.GroupTitle))
                    {
                        <div style="padding:7px">

                             @if (row.ControlType == "Single Line Text")
                             {
                                <label>@row.Caption</label>
                                <input type="text" name="@row.Name" class="@row.Mandatory" />
                             }
                             else if (row.ControlType == "Multi Line Text")
                             {
                                 <label>@row.Caption</label>
                                 <textarea name="@row.Name" class="@row.Mandatory"></textarea>
                             }
                             else if (row.ControlType == "Yes/No Choice(Radio Buttons)")
                             {                            
                                <div>     
                                    <label>@row.Caption</label>
                                    &nbsp                     
                                    <input type="radio" name="@row.Name" value="Yes" class="@row.Mandatory"/> &nbsp Yes                             
                                    &nbsp
                                    <input type="radio" name="@row.Name"  value="No" class="@row.Mandatory"/> &nbsp No
                                </div>
                             }
                             else if (row.ControlType == "Checkbox")
                             {
                                 <div>
                                    <input type="checkbox" name="@row.Name" class="@row.Mandatory"/> @row.Caption
                                 </div>
                             }
                             else if (row.ControlType == "Date")
                             {
                                 <div>
                                     <label>@row.Caption</label>
                                    <input type="date" name="@row.Name" class="@row.Mandatory"/>
                                 </div>
                             }
                        </div>
                    }
                </div>
                }

脚本

<script type="text/javascript">
    $(document).ready(function () {
     $('#signupform').submit(function ()
     {
        $(".validate").each(function () {                
            if ($(this).is("input[type=radio]")) {        

               alert($(this).attr('name'));
              // here is the logic to check whether user has selcted Yes or No

            }
        });
   });
});

3 个答案:

答案 0 :(得分:2)

用作

 $('input[name="rbc"]:checked').val()

DEMO

答案 1 :(得分:0)

尽量避免jQuery中的伪选择器([type = radio])它们的性能不佳。使用类选择器。将课程放入您的收音机,然后执行此操作:

$('.my-class:checked').val();

如果您不想使用:直接检查,您可以将其用作

$('.my.class').each(function(){
     if ( $(this).is(':checked') ) { /* magic */ }
});

答案 2 :(得分:0)

您应该只为您的单选按钮获取:checked项:

$(function () {
    $('button').on('click', function () {
        $('.validate:checked').each(function () {
            var name = $(this).attr('name');
            var value = $(this).val();
            alert(name + ' : ' + value);
        });
    });
});

JSFiddle Demo