有没有一种方法来连接在HTML属性字符串? [英] Is there a way to concatenate strings in html attributes?

查看:96
本文介绍了有没有一种方法来连接在HTML属性字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC3,我想用一个局部视图创建动态DOM元素。这是我目前的局部视图:

I'm using MVC3 and I wanted to use a partial view to create dynamic DOM elements. This is my current partial view:

@model MVCApp.ViewModels.TitlesViewModel

<div class="display-label">Name</div>
<div id="label"+"@Model.Id" class="display-field">@Model.InitValue</div>

Model.Id是1,但在在浏览器的HTML,我目前得到:

Model.Id is 1 but in the HTML in the browser, I currently get:

id="label"+"1"

所以,如果我尝试做这样的事情:

So if I try and do something like:

alert($("#label1").text())

有与什么也没有一个警告框。

There's an alert box with nothing in it.

所以,我怎么可以添加两个字符串在一起,形成由jQuery的(或的document.getElementById(STR)为此事)认得一份连贯的字符串。

So how can I add the two strings together to form one coherent string that is recognized by jQuery (or document.getElementByID(str) for that matter).

推荐答案

试试这个(认证):

<div id="@("label"+Model.Id)" class="display-field">@Model.InitValue</div>

这篇关于有没有一种方法来连接在HTML属性字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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