Url.Content 中的 Javascript 变量

编程入门 行业动态 更新时间:2024-10-24 04:37:00
本文介绍了Url.Content 中的 Javascript 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个名为 locid 的 javascript 变量,我试图通过如下设置 div 的 data-url 将其用作 URL 的一部分:

I have a javascript variable called locid that I am trying to use as part of a URL by setting the data-url of a div as follows:

data-url='@Url.Content("~/Catalogue_Items/ItemsActionViewPartial/")@Model.CatItemID?LocationID=locid&AsyncUpdateID=catalogue-view'>

我知道我不能像我在示例代码中所做的那样只在该字符串中抛出 locid,但我只是把它放在那里来说明我需要我的 javascript 变量的位置.我怎样才能做到这一点??

I know I can't just throw the locid in that string as I have done in my sample code but I just put it there to illustrate where I need my javascript variable to be. How can I achieve this??

推荐答案

您不能直接将 Javascript 变量使用到属性中.一种解决方法是重新排列您的 url 参数并在脚本标记中设置 JavaScript 变量.

You cannot use the Javascript variable directly into attribute. A workaround can be to reorder the parameters of your url and setting the JavaScript variable in script tag.

<button id="myButton" data-url='@Url.Content("~/Catalogue_Items/ItemsActionViewPartial/")@Model.CatItemID?AsyncUpdateID=catalogue-view'></button>

我在这里重新排序了 url 参数.位置参数将通过以下脚本设置.将此脚本放在按钮元素之后.

I have reordered the url parameters here. The location parameter will be set by following script. Place this script just after the button element.

<script> var button = document.getElementById('myButton'); var url=button.getAttribute('data-url'); url+="&LocationID=" + locid; button.setAttribute('data-url',url); </script>

更多推荐

Url.Content 中的 Javascript 变量

本文发布于:2023-11-02 08:14:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1551853.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   Url   Content   Javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!