【发布时间】:2015-05-18 17:12:08
【问题描述】:
我收到提要列表。每个提要都有唯一编号 feed.id。
<c:forEach items="${feedList}" var="feed">
<td><input readonly="readonly" id="feedId"
value="<c:out value="${feed.id}" />" /></td>
<td><c:out value="${feed.name}" /></td>
<td><a
href="FeedController?action=delete&id=<c:out value="${feed.id}"/>">Delete</a></td>
<td><a
href="FeedItemController?action=feedItemListAsc&id=<c:out value="${feed.id}"/>">View</a>
</td>
<td><a href="#dialog" name="modal">Edit</a></td>
</c:forEach>
按编辑打开带有输入名称的模式窗口:
$('a[name=modal]').click(function(e) {
//Cancel the link behavior
e.preventDefault();
//Get the A tag
var id = $(this).attr('href');
//Get the screen height and width
var maskHeight = $(document).height();
var maskWidth = $(window).width();
//Set height and width to mask to fill up the whole screen
$('#mask').css({
'width' : maskWidth,
'height' : maskHeight
});
//transition effect
$('#mask').fadeIn(1000);
$('#mask').fadeTo("slow", 0.8);
//Get the window height and width
var winH = $(window).height();
var winW = $(window).width();
//Set the popup window to center
$(id).css('top', winH / 2 - $(id).height() / 2);
$(id).css('left', winW / 2 - $(id).width() / 2);
//transition effect
$(id).fadeIn(2000);
});
<div id="boxes">
<div id="dialog" class="window">
<table class="userInput" border="1">
<tr>
<td colspan="2"><input type="text" id="feedName" />
</td>
</tr>
<tr>
<td><input type="submit" id="renameFeed" value="Submit"></td>
<td>
<a href="#" class="close">Close it</a>
</td>
</tr>
</table>
</div>
<div id="mask"></div>
</div>
最后使用 ajax 从模态窗口中获取名称,并从 forEach 块中获取 id 参数:
$('#renameFeed').click(function() {
var name = $('#feedName').val();
var id = $('#feedId').val();
var action = 'edit';
var data = "feedName=" + name + "&id=" + id + "&action=" + action;
$.ajax({
type : "Get",
url : "FeedController",
data : data,
success : function(result) {
if (typeof (result) != 'undefined') {
}
},
error : function(error) {
console.log("error" + error);
}
});
});
之后,我想使用 servlet 和我的 html 页面更改数据库中的名称。
我需要在按下编辑按钮时为每个获取 feed.id,然后在按下 <input type="submit" id="renameFeed" value="Submit"> 时将值发送到 servlet。
可以轻松获取 feed.name 但传递 feed.id 有问题
也许有人知道使用模态窗口和 jQuery ajax 重命名的更好解决方案?
【问题讨论】:
-
id 应该是唯一的。尝试通过添加数字来动态生成。
-
这个 id 随列表中的每个提要一起提供,我只是显示它。在我的情况下,我需要检索它并在 ajax 中使用,但在为不同的提要按下编辑按钮时始终具有相同的 id
-
您也需要发布用于打开对话框的功能..
标签: javascript jquery ajax jsp servlets