简体   繁体   中英

Assign a dynamic string to the ID of an element

I want to assign a dynamic string to the ID of an element. Here's how my input element is:

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

notice, I am assigning 'ac' + @item.index

@item.index is the dynamic part, it is an integer.

I can assign @item.index and it works, however, when I try to combine the string 'ac' to @item.index, it does not work.

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

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

The issue is that ac is going to be a plain string in 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

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

This way it would be crystal clear what exactly you are doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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