在asp.net mvc中使用javascript为li的Onclick事件 [英] Onclick event for li with javascript in asp.net mvc
本文介绍了在asp.net mvc中使用javascript为li的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点击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;
});
});
这篇关于在asp.net mvc中使用javascript为li的Onclick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文