简体   繁体   English

将动态字符串分配给元素的ID

[英]Assign a dynamic string to the ID of an element

I want to assign a dynamic string to the ID of an element. 我想为一个元素的ID分配一个动态字符串。 Here's how my input element is: 这是我的输入元素是:

<input id = 'ac'+ @item.index />

notice, I am assigning 'ac' + @item.index 注意,我正在分配'ac'+ @ item.index

@item.index is the dynamic part, it is an integer. @ item.index是动态部分,它是一个整数。

I can assign @item.index and it works, however, when I try to combine the string 'ac' to @item.index, it does not work. 我可以指定@ item.index,但是,当我尝试将字符串'ac'组合到@ item.index时,它不起作用。

What am I doing wrong? 我究竟做错了什么?

Thank you. 谢谢。

Try using an explicit code nugget . 尝试使用显式代码块 Basically just wrap it in brackets. 基本上只需将其包装在括号中。 You'll also want quotes around your attribute value. 您还需要围绕属性值的引号。

Try this 尝试这个

<input id="ac@(item.index)" />

Try changing the integer to a string then combining them 尝试将整数更改为字符串,然后组合它们

Edit sorry the first one I posted was was for Java, here's the javscript 编辑抱歉,我发布的第一个是Java,这是javscript

'ac' + @item.index.toString(); 'ac'+ @ item.index.toString();

The issue is that ac is going to be a plain string in html. 问题是ac将成为html中的普通字符串。 So is what follows it (the +). 接下来是什么(+)。

Instead, you should just plug it right in (remember it will literally write right there) 相反,你应该直接插入(记住它会直接写在那里)

<input id = 'ac@(item.index)' />

I would use string.Format here 我会在这里使用string.Format

   <input id='@string.Format("ac{0}", item.index)' />

This way it would be crystal clear what exactly you are doing. 这样就可以清楚地知道你在做什么。

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

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