简体   繁体   English

使用Javascript更改asp.net核心TagHelpers

[英]Changing asp.net core TagHelpers with Javascript

I have 我有

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

in my asp.net core application, that generate this html when compiled: 在我的asp.net核心应用程序中,在编译时生成此html:

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

How can i change asp-route-id value from javascript? 如何从javascript更改asp-route-id值? I tried with $().attr but it's not recognized. 我尝试使用$().attr但它无法识别。

You have to change your href attribute in generated html. 您必须在生成的html中更改href属性。

You can achive this by geting your href attribute, split it into array, then change value in array and join it again into one string with separator and then replace href attribute in your a element. 您可以通过创建href属性,将其拆分为数组,然后更改数组中的值并再次将其连接到带有分隔符的一个字符串,然后替换元素中的href属性来实现此目的。

Code example: 代码示例:

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('/'));

Check this codepen to see how it work. 检查此codepen以了解它是如何工作的。

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

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