Li在ASP.NET MVC中使用JavaScript的Onclick事件 [英] Onclick event for li with javascript in asp.net mvc
本文介绍了Li在ASP.NET MVC中使用JavaScript的Onclick事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<ul>
@foreach (var item in Model.Users.Where(s => s.DepartmentId == 3))
{
<li>
@Ajax.ActionLink(item.UserName, "Index", new { item.Id }, new AjaxOptions() { HttpMethod = "Get" });
</li>
}
</ul>
如果我使用javascript onclick item.username,如何获得item.Id?
How can ı get item.Id if i onclick item.username with javascript ?
我以为将属性赋予"li onclick".但是我不知道这里是否有更好的解决方案?
I thought give to attribute to "li onclick".but i dont know there is better solution here or not ?
推荐答案
您可以使用标准链接和data- *属性:
You could use a standard link and data-* attribute:
<ul>
@foreach (var item in Model.Users.Where(s => s.DepartmentId == 3))
{
<li>
@Html.ActionLink(
item.UserName,
"Index",
new { id = item.Id },
new { @class = "myLink", data_id = item.Id }
)
</li>
}
</ul>
,然后在单独的javascript文件中:
and then in a separate javascript file:
$(function() {
$('.myLink').click(function() {
// get the id:
var id = $(this).data('id');
alert(id);
// now you can send the AJAX request
$.get(this.href);
// cancel the default action of the link
return false;
});
});
这篇关于Li在ASP.NET MVC中使用JavaScript的Onclick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文