从表格中的下拉列表中获取选定的值

时间:2015-01-29 12:26:33

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

我尝试从表格中的下拉列表中获取值,每次点击都会获得相同的值.....

 <tbody>

        @{
            var tempId=1;
            foreach (var item in lst)
            {
                var username = _lst.Where(x => x.UserName == @item.UserName);
                if (username.Count() > 0)
                {
                    <tr>
                        <td><input type="hidden" id="@tempId" name="tempId_@tempId"></td>
                        <td>@username.Select(x => x.UserName).FirstOrDefault() </td>
                        <td class="nr"> @*@Html.DropDownList("UserRoleTypeId", "Select")*@

                        @Html.DropdownListCustom("UserRoleTypeId" + @tempId, "", (IEnumerable<SelectListItem>)ViewBag.UserRoleTypeId)
                        </td>
                        <td>
                            @Html.Hidden("Id", i = username.Select(x => x.Id).FirstOrDefault())
                        </td>
                        <td><input type="button" data-id="@item.UserName" value="set" class="btn-update" />  </td>
                    </tr>
                }
                else
                {
                    <tr>
                        <td></td>
                        <td>@item.UserName</td>
                        <td class="nr"> @*@Html.DropDownList("UserRoleTypeId", "Select")*@
                            @*@Html.DropdownListCustom("UserRoleTypeId_" + @tempId, "", (IEnumerable<SelectListItem>)ViewBag.UserRoleTypeId, new { @id = "UserRoleTypeId" })*@
                       @Html.DropdownListCustom("UserRoleTypeId" + @tempId, "", (IEnumerable<SelectListItem>)ViewBag.UserRoleTypeId)
                        </td>
                        <td>
                            @Html.Hidden("Id", i)
                        </td>
                        <td><input type="button" data-id="@item.UserName" value="set" class="btn-update" /></td>
                    </tr>
                }
                tempId = tempId + 1;
            }
        }

    </tbody>

jquery代码是:

 <script type="text/javascript">
        $(function () {
            //var UserRoleTypeId = 0;
            $('.btn-update').click(function () {
                debugger;
                var element = this;
                var root = '@Url.Content("~")';
                UserRoleTypeId = $("#UserRoleTypeId option:selected").val();
                var Id = $("#Id").val();
                alert(UserRoleTypeId);
                $.ajax({
                    type: 'Get',
                    url: root + 'AssginRole/UpdateRole',
                    data: { 'userId': $(this).attr('data-id'), 'UserRoleTypeId': UserRoleTypeId, 'Id': Id },
                    dataType: 'json',
                    success: function () {
                        $('#loadingElement').hide();
                        alert("Permission Set Sucessfully");
                    },
                    error: function (req, status, err) {
                        alert('unable to update the role, sorry, pls. try again... ' + err);
                        $('#loadingElement').hide();
                    }
                });
            });
        });
    </script>}

on Top i&#39; m创建一个表格,我有下拉列表和文本。我需要的是按钮点击获取特定的行值。我尝试但我只得到第一个td值(用户名)

1 个答案:

答案 0 :(得分:3)

尝试此操作以访问dropdownlist

 UserRoleTypeId = $(this).parent().siblings().find("select").val();