如何使用 MVC2 使网页组件(Div、a、h1 等)的 ID 动态(变量?)?
我正在尝试执行以下操作: 我提供给 Viewpage 的对象有一个列表,我在 HTML 中执行 foreach 并创建了许多组件。现在,我希望这些组件的 ID 以某种方式链接到列表中的对象。
(我尝试这样做的原因是因为我想显示一个按钮,当他们按下该按钮时,显示的内容将会改变,他们应该看到其他东西,我将使用javascript来实现这一点)
因此我试图使这些组件的 id 是动态的,例如声明id="button <%= item.id%>
" 但这似乎不起作用。我在谷歌上搜索了很多,但还没有找到解决方案,这就是为什么我向你们求助。
我也会链接我的代码,我删除了一些不必要的部分(但添加了 javascript):
<script type="text/javascript">
function AlterPanel(thePanel) {
var panel = document.getElementById("region"+thePanel);
panel.style.display = 'block';
var button = document.getElementById("button"+thePanel);
button.style.display = 'none';}
</script>
<%foreach (TeamDTO team in Model.List.Teams)
{ %>
<a id="button<%= team.Number %>" onclick="AlterPanel(<% team.Number%>)">
Add member</a>
<div Visible="false" id='region<%= team.Number %>' runat="server">
Please select one:
<%: Html.DropDownListFor(V => V.memberID, new SelectList(Model.members, "ID","Name")) %>
</div>
<% } %>
我热切地等待回复并提前感谢您。
I am trying to do the following:
The object I give to the Viewpage has a list, I do a foreach in the HTML and I create a number of components. Now, I want the IDs of those components to somehow be linked to the object from the List.
(The reason I am trying to do this is because I want to show a button, when they press that button the shown content will change and they should see something else, I will use javascript to achieve this)
Therefor I am trying to make the id of those components dynamic, by for example statingid="button <%= item.id%>
" however this does not seem to work. I have searched alot on google but I haven't found a solution yet which is why I turn to you guys.
I'll link my code as well, I deleted some of the parts that were unnecessary (but added the javascript):
<script type="text/javascript">
function AlterPanel(thePanel) {
var panel = document.getElementById("region"+thePanel);
panel.style.display = 'block';
var button = document.getElementById("button"+thePanel);
button.style.display = 'none';}
</script>
<%foreach (TeamDTO team in Model.List.Teams)
{ %>
<a id="button<%= team.Number %>" onclick="AlterPanel(<% team.Number%>)">
Add member</a>
<div Visible="false" id='region<%= team.Number %>' runat="server">
Please select one:
<%: Html.DropDownListFor(V => V.memberID, new SelectList(Model.members, "ID","Name")) %>
</div>
<% } %>
I eagerly await a reply and thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜你的问题是:
-您有一些“按钮 DropDownList”对,按钮可见,DropDownList 不可见,现在如果用户单击按钮,则 DropDownList 将显示。
好吧,现在你的视图可能是:
我在 javascript 部分使用 JQuery,如下所示:
不要忘记在 View() 中包含以下文件:
如果答案不是你想要的,请告诉 mt,我可以帮助你〜:)
I guess your Queastion is:
-you have some "button DropDownList" pair, button is visiable, DropDownList is invisible, now if user click the button then DropDownList will showup.
OK, now your View maybe :
I use JQuery in javascript part like this:
Don't forget include the following file in View():
if the answer is not what you want, let mt know and I can help you~ :)