有很多类似的问题,但它们都是关于如何编写动态创建的ID标记,而不是现在在尝试使用ID时引用它们。下面,我发布了第一个使用<tr>
标记中的SessionId创建带有ID标记的表的函数(请参阅代码注释“这是重要的部分”。
下面我有我希望它做的功能。我试图在单击该行中的按钮后更改表格行中的内容。
我有这个代码创建标签:
$('#lookup').click(function (event) {
$('#results').html("");
$('#btn').html("");
$('#reset').html("");
strUser = $('#userid').val();
lookfor = JSON.parse('{ "username":"' + strUser + '" }');
$.ajax({
type: "POST",
url: "/Home/FindUser",
data: lookfor,
}).done(function (data) {
var html = "";
var err = "";
var count = 0;
var elite = false;
$.each(data, function (entryIndex, entry) {
if (entryIndex == 0) {
html += "<tr>";
html += "<th>Username</th>";
html += "<th>Server</th>";
html += "<th>App</th>";
html += "</tr>";
}
if (entry.Result) {
err = "<tr>";
err += "<td>" + entry.Result + "</td>";
err += "</tr>";
} else { //**This is the important part here**:
html += "<tr id=Session" + entry.SessionId + ">";
html += "<td><input type=\"hidden\" name=\"sessid\" value=\"" + entry.SessionId + "\" id=\"SessionId\">" + entry.UserId + "</td>";
html += "<td><input type=\"hidden\" name=\"sessid\" value=\"" + entry.ServerName + "\" id=\"ServerName\">" + entry.ServerName + "</td>";
html += "<td><input type=\"hidden\" name=\"sessid\" value=\"" + entry.AppName + "\" id=\"AppName\">" + entry.AppName + "</td>";
html += "<td><a href=\"/Home/Details?SessionId=" + entry.SessionId + "&ServerName=" + entry.ServerName + "\">Details</a></td>";
html += "<td><input type=\"submit\" value=\"Log Off\" runat=\"server\" class=\"btn btn-default\" id=\"logoffsession\" /></td>";
html += "</tr>";
}
if (entry.Elite) {
elite = true;
}
count++;
});
if (err.length > 0) {
$('#results').append($(err));
} else {
$('#results').append($(html));
}
if (count > 0) {
var btn = "<td><input type=\"submit\" value=\"Log Off All Sessions\" runat=\"server\" class=\"btn btn-default\" id=\"logoff\" /></td>";
$('#reset').append($(btn));
} else if (elite) {
var btn = "<td width=\"50\"><input type=\"submit\" value=\"Reset Elite Profile\" runat=\"server\" class=\"btn btn-default\" id=\"resetelite\" /></td>";
$('#reset').append($(btn));
} else {
}
});
});
现在这就是我想用它做的事情:
$(document).ready(function () {
$(document).on("click", "#logoffsession", function (event) {
var SessionId = $('#SessionId').val();
var ServerName = $('#ServerName').val();
var AppName = $('#AppName').val();
var Pattern = /Elite/;
var exists = Pattern.test(AppName);
lookfor = JSON.parse('{ "sessionid":"' + SessionId + '","servername":"' + ServerName + '" }');
$.ajax({
type: "POST",
url: "/Home/LogOffSession",
data: lookfor,
}).done(function (data) {
var html = "<td>" + data.Result + "</td>"
//**Right here below is what I want to do**:
$("'#Session" + SessionId + "'").html("");
$("'#Session" + SessionId + "'").append($(html));
});
});
});
$("'#Session" + SessionId + "'")
调用它似乎不起作用。