繁体   English   中英

使用Javascript更改asp.net核心TagHelpers

[英]Changing asp.net core TagHelpers with Javascript

我有

 <a id="continue-link" asp-controller="Account" asp-action="Register" asp-route-id="1">Continue </a>

在我的asp.net核心应用程序中,在编译时生成此html:

<a href="/Account/Register/1" id="continue-link">Continue </a>

如何从javascript更改asp-route-id值? 我尝试使用$().attr但它无法识别。

您必须在生成的html中更改href属性。

您可以通过创建href属性,将其拆分为数组,然后更改数组中的值并再次将其连接到带有分隔符的一个字符串,然后替换元素中的href属性来实现此目的。

代码示例:

var $link = $('#continue-link');
var href = $link.attr('href').split('/');
href[3] = 4; //here you set your new asp-route-id value
$link.attr('href', href.join('/'));

检查此codepen以了解它是如何工作的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM