如何使用jquery拦截ActionLink
我想从隐藏字段向ActionLink添加参数。我找到了解释它的答案,但对我来说不起作用,因为它直接进入控制器操作,而不解析jquery代码如何使用jquery拦截ActionLink,jquery,asp.net-mvc-4,actionlink,Jquery,Asp.net Mvc 4,Actionlink,我想从隐藏字段向ActionLink添加参数。我找到了解释它的答案,但对我来说不起作用,因为它直接进入控制器操作,而不解析jquery代码 @Html.ActionLink(CommonResource.Delete, "PayrollPeriodDelete", "CodeTable", null, new { @id = "sortlink", @class = "btn btn-success" }) &l
@Html.ActionLink(CommonResource.Delete, "PayrollPeriodDelete", "CodeTable", null, new { @id = "sortlink", @class = "btn btn-success" })
<script type="text/javascript">
$(function () {
$('#PayrollPeriodSearch_FromDate').datepicker();
$('#PayrollPeriodSearch_ToDate').datepicker();
});
$("#sortlink").click(function()
{
alert("works");
var $lat = $("#hiddencode").val();
$(this).attr("href", $(this).attr("href") + "?id=" + $lat);
});
</script>
@Html.ActionLink(CommonResource.Delete,“PayrollPeriodDelete”,“CodeTable”,null,新的{@id=“sortlink”,@class=“btn btn success”})
$(函数(){
$(“#薪资周期搜索_FromDate”).datepicker();
$('#PayrollPeriodSearch_ToDate').datepicker();
});
$(“#sortlink”)。单击(函数()
{
警惕(“工作”);
var$lat=$(“#hiddencode”).val();
$(this.attr(“href”),$(this.attr(“href”)+“?id=“+$lat”);
});
我可以告诉你我的jquery是有效的,因为上面的函数运行得很好。
请提供帮助。您需要使用指定在DOM完全加载时必须执行函数:
<script type="text/javascript">
$(document).ready(function () {
$("#sortlink").click(function()
{
alert("works");
var $lat = $("#hiddencode").val();
$(this).attr("href", $(this).attr("href") + "?id=" + $lat);
});
}
</script>
$(文档).ready(函数(){
$(“#sortlink”)。单击(函数()
{
警惕(“工作”);
var$lat=$(“#hiddencode”).val();
$(this.attr(“href”),$(this.attr(“href”)+“?id=“+$lat”);
});
}
试试这个
$(document).on('click','#sortlink',function(ev)
{
ev.preventDefualt(); // this will disable default action of action link...
alert("works");
var $lat = $("#hiddencode").val();
window.location = $(this).attr("href") + "?id=" + $lat;
});