Jquery 从表中的下拉列表中获取选定值
我试图从表中的下拉列表中获取值,每次单击都会得到相同的值Jquery 从表中的下拉列表中获取选定值,jquery,asp.net,asp.net-mvc,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(
<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>
@{
var-tempId=1;
foreach(lst中的var项目)
{
var username=_lst.Where(x=>x.username==@item.username);
如果(username.Count()>0)
{
@username.Select(x=>x.username).FirstOrDefault()
@*@DropDownList(“UserRoleTypeId”,“Select”)*@
@DropdownListCustom(“UserRoleTypeId”+@tempId,”,(IEnumerable)ViewBag.UserRoleTypeId)
@Html.Hidden(“Id”,i=username.Select(x=>x.Id).FirstOrDefault()
}
其他的
{
@item.UserName
@*@DropDownList(“UserRoleTypeId”,“Select”)*@
@*@Html.DropdownListCustom(“UserRoleTypeId”+@tempId,”,(IEnumerable)ViewBag.UserRoleTypeId,新的{@id=“UserRoleTypeId”})*@
@DropdownListCustom(“UserRoleTypeId”+@tempId,”,(IEnumerable)ViewBag.UserRoleTypeId)
@Html.Hidden(“Id”,i)
}
tempId=tempId+1;
}
}
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>}
$(函数(){
//var UserRoleTypeId=0;
$('.btn更新')。单击(函数(){
调试器;
var元素=这个;
var root='@Url.Content(“~”);
UserRoleTypeId=$(“#UserRoleTypeId选项:选中”).val();
var Id=$(“#Id”).val();
警报(UserRoleTypeId);
$.ajax({
键入:“Get”,
url:root+'AssginRole/UpdateRole',
数据:{'userId':$(this).attr('data-id'),'UserRoleTypeId':UserRoleTypeId,'id':id},
数据类型:“json”,
成功:函数(){
$('#loadingElement').hide();
警报(“权限设置成功”);
},
错误:功能(请求、状态、错误){
警报('无法更新角色,抱歉,请重试…'+错误);
$('#loadingElement').hide();
}
});
});
});
}
在顶部,我正在创建一个表格,其中有下拉列表和文本。我需要的是在按钮上单击“获取特定行值”。我正在尝试,但只获取第一个td值(用户名)尝试访问
下拉列表
UserRoleTypeId = $(this).parent().siblings().find("select").val();
尝试访问
dropdownlist
UserRoleTypeId = $(this).parent().siblings().find("select").val();
id
在HTML中必须是唯一的。这就是您面临问题的原因使用类选择器而不是什么是DropdownListCustom
?通过使用重复的id
属性id
生成的无效html在html中必须是唯一的。这就是您面临问题的原因使用类选择器而不是什么是DropdownListCustom
?看起来您通过使用重复的id
attributesHanks buddy g8 job:)生成的html无效。谢谢buddy g8 job:)