我有一个名为 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 变量
发布评论