Url.Content 中的 Javascript 变量 [英] Javascript Variable in Url.Content

查看:64
本文介绍了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 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆