gpt4 book ai didi

jquery - 如何使用jquery调用html.actionlink

转载 作者:行者123 更新时间:2023-12-03 23:04:58 30 4
gpt4 key购买 nike

我想使用jquery调用actionlink,下面是代码:

  $("#paycheck").click(function () {

if ($("#terms").attr("checked")) {

//Call Html.ActionLink // This is where the html.ActionLink should be called to display another view


} else {
alert("Please agree to the terms and conditions.");
return false;
}
});

<%: Html.ActionLink("Pay", "Index", "News") %>

最佳答案

您不能使用 jQuery调用操作链接。您可以向此链接指向的 Controller 操作发送 AJAX 请求。如果这是您想要的,以下是实现方法它:

$(function() {
$('#paycheck').click(function () {
if ($('#terms').is(':checked')) {
// Send an AJAX request to the controller action this link is pointing to
$.ajax({
url: this.href,
type: 'GET',
// you can send some additional data along with the request
data: { foo: 'bar' },
success: function(result) {
// TODO: process the results returned by the controller
}
});
} else {
alert('Please agree to the terms and conditions.');
}
return false;
});
});

另请确保在生成操作链接时为操作链接提供正确的 ID (paycheck)

<%= Html.ActionLink("Pay", "Index", "News", null, new { id = "paycheck" }) %>

但是,如果只是检查用户是否接受条款和条件,然后执行标准重定向到 Controller 操作而不使用任何 AJAX,只需执行以下操作:

$(function() {
$('#paycheck').click(function () {
if ($('#terms').is(':checked')) {
// by returning true you are letting the browser redirect to the link
return true;
}

alert('Please agree to the terms and conditions.');
// By returning false you stay on the same page and let the user
// agree with the terms and conditions
return false;
});
});

关于jquery - 如何使用jquery调用html.actionlink,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6691154/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com