這是我的div標籤如何連接按鈕的ID與綁定值的jQuery
<div id="generate">
</div>
我的動態生成HTML有,我曾與唯一的消息ID綁定,所以我可以區分按鈕的ID,而輸入型按鈕控制點擊。
<input type="button" onclick="return GetSelectedRow(this);" class="btn" value="Post" id="b' +<%#Eval("id")%>'"/>
這是詳細的代碼。
$(document).ready(function() {
debugger;
var generateHere = document.getElementById("generate");
debugger;
generateHere.innerHTML = '<div class="card w-post"> <div class="alert alert-danger hide" id="AlertDiv" runat="server"> <asp:Label ID="ErrorMessageLabel" runat="server" CssClass="control-label" Text=""></asp:Label></div><div class="card-body"><textarea class="wp-text auto-size" id="TextBox1" placeholder="Write Something..."></textarea><div class="tab-content p-0"></div><div class="wp-actions clearfix"><div class="wpa-media-list pull-left"></div> <input type="button" onclick="SaveMessageRecord(); return false" class="btn" value="Post" id="btnInsertRecord"/> </div> </div> </div><div id="result"><div id="hello"><div id="Div1"><div class="card w-item"><div class="card-header"> <div class="media"><div class="pull-left"><img class="avatar-img" src="img/profile-pics/ACER.gif" alt=""/></div><div class="media-body" id="usernames"><h2><%#Eval("username")%></h2><small><%#Eval("posteddate")%></small><h5 id="msgids"><%#Eval("id")%></h5></div> </div></div><div class="card-body card-padding"><p><%#Eval("message") %></p><div class="wis-numbers"><span><i class="zmdi zmdi-comments"></i></span></div><div class="wi-stats clearfix"><div></div></div></div><div class="wi-comments"><div class="list-group" id="result2"><div class="list-group-item media"><div class="pull-right"> </div> <div class="media-body" id="welcome"> <a href="#" class="lgi-heading"><small class="c-gray m-l-10"></small></a><small class="lgi-text"><%#Eval("comments")%></small></div></div></div><div class="wic-form" id="commentsform"><textarea class="wp-text auto-size" data-ma-action="wall-comment-open" id="TextBox1" placeholder="Write Comments..."></textarea><div class="wicf-actions text-right"><button class="btn btn-sm btn-link" data-ma-action="wall-comment-close">Cancel</button><input type="button" onclick="return GetSelectedRow(this);" class="btn" value="Post" id="b' +<%#Eval("id")%>'"/></div></div></div></div></div></div></div>';
});
現在對於克隆div標籤我用
$(function() {
$.ajax({
type: "POST",
url: "RepeaterAjax.aspx/GetMessages",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
});
function OnSuccess(response) {
debugger;
var xmlDoc = $.parseXML(response.d);
var xml = $(xmlDoc);
var welcome = xml.find("Table");
var table = $("#hello div").eq(0).remove();
welcome.each(function() {
debugger;
var welcome = $(this);
var msgid = $("h5", table).html(welcome.find("id").text());
$("p", table).html(welcome.find("message").text());
$("h2", table).html(welcome.find("username").text());
$("#usernames small", table).html(welcome.find("posteddate").text());
$("h5", table).html(welcome.find("id").text());
$("#commentsform input[type=button]", table).html(welcome.find("id").text());
$("#hello").prepend(table).prepend("<br/>");
table = $("#hello div").eq(0).clone(true);
});
}
我的問題是,我不能與MSGID這是唯一的級聯動態生成的按鈕。所以,雖然點擊我可以區分生成的div ..
'welcome.find(「id」)。text()' - 你是否期待這找到按鈕ID?它不會,它會尋找元素'' –
ADyson
,因爲ADyson說,而不是使用doucment.getElementById(); –
我的查詢是如何綁定在上面的詳細代碼中找到的id,它是唯一的,並且在運行時由$(「h5」)生成,如
<%#Eval(「id」)%>
「 ,table).html(welcome.find(「id」)。text());到'「/>清楚地說,每次消息被插入動態div創建的地方。一個按鈕的動態div呈現...我想按鈕ID綁定唯一的ID.So按鈕ID可以是唯一的,並識別動態生成的div和檢索值很容易.. – havin