简体   繁体   English

Loop Div内的Loop按钮

[英]Loop Button inside the Loop Div

I'm new here, I'm stuck in problem in which i have to loop my button so that i will be able to put it on my looping div. 我是新来的人,我陷入了一个问题,我必须循环我的按钮,这样我才能将其放在循环div上。 How will I do that? 我该怎么做? This is what I got so far: //my loop 这是到目前为止我得到的://我的循环

for (i = 0; i < MPreply.recommendation.Length; i++) {
   html += "<div class='rec'> ";
   html += "</br> <div id = 'from1' value =  > Depart From:" + " ";
   html += emz + " " + "(" + + ")" + "</div>";
   html += "<div id = 'date1' > Schedule:" + " " + flytDate;
   html += " , " + convertedString;
   html += "</div>" + "<div id = 'to1' >To:" + " " + emz2 + " " + "(" + "" + ")";
   html += "</div> </br> </br> </br> ";
   html += "<div id = 'RecNo'>" + " " + (i + 1) + " )" + " </br> </br> </div> ";
   html += "<div id = 'fare1'> $" + em + "  </div> ";
   html += " </div>  <br/> <br/>";
   div_rec.InnerHtml = html;
}

html: 的HTML:

<div id = 'modal' runat="server" >
        <div id="dialog" runat="server">

        </div>
        <input id="_menuitem" type="button" value="Click" runat="server" />
    </div>  

Thanks in advance. 提前致谢。

You have at least a few problems: 您至少有一些问题:

"<div id = 'fare1'>

and similar sections will generate multiple elements with the same id. 并且类似的部分将生成具有相同ID的多个元素。

div_rec.InnerHtml = html;

Should be moved outside of your loop so that div_rec is only updated once , when everything is done. 应该将其移出循环,以便在完成所有操作后div_rec仅更新一次

emz + " " + "(" + + ")" + "</div>";

Should (probably) be changed to 应该(可能)更改为

emz + " " + "(" + your_variable_here + ")" + "</div>";

In order to (I think) add the creation of a button in your loop, you'll have to piece it together as a string just like your other elements. 为了(我认为)在循环中添加按钮的创建,您必须像其他元素一样将其拼成一个字符串。 To get a function handler wired up you'll have to either: 要连接功能处理程序,您必须:

a) Build a dom level-0 handler as a string, or a)将dom级别0处理程序构建为字符串,或者

b) Wait until your html is updated, then query out the newly added buttons and add event handlers in code (using either btn.onclick = function(... or dom level-2 handlers (addEventListener or attachEvent) b)等待直到您的html更新,然后查询新添加的按钮并在代码中添加事件处理程序(使用btn.onclick = function(...或dom level-2处理程序(addEventListener或attachEvent)

The Problem i see here is with the ID field. 我在这里看到的问题是ID字段。

You have mentioned same ID name for all the Div's you are generating. 您为正在生成的所有Div提到了相同的ID名称。

Like, 喜欢,

<div id = 'from1'>
<div id = 'to1'>

ID is a unique identifier to an element. ID是元素的唯一标识符。 It must always be unique. 它必须始终是唯一的。

You can Use Class instead. 您可以改用Class。

Like, 喜欢,

<div class = 'from1'>
<div class = 'to1'>

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

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