jQuery:无法检索动态创建的 DOM 元素的属性
情况如下:我正在编写一个执行 CRUD 功能的简单 AJAX 应用程序。当用户双击特定元素时,该元素会变为文本框,以便他们可以进行内联编辑。当文本框失去焦点(其代码如下)时,文本框的值将被 POST 到更新数据库的 PHP 脚本。
除了一件事之外,一切都很精彩。当我创建一条新记录时,该记录会通过 AJAX 弹出到列表顶部,如果不刷新页面,我将无法编辑该记录。我的意思是,编辑看起来就像已提交,但是当您刷新时,它会恢复为原始状态。刷新后就没有问题了。
归结为:当我尝试在表中(数据库和页面上)新创建的行上运行以下代码时(在数据库中和页面上),编辑似乎是在页面上创建,但从未进入数据库。
//Make changes on FOCUSOUT
$('#editable').live('focusout', function(){
var parentListItem = $(this).parents('li');
var theText = $(this).val();
var parentListItemID = parentListItem.parents('ul').attr('id');
$(this).remove();
parentListItem.html(theText);
parentListItem.removeClass('beingEdited');
$.post("databasefuncs.php?func=edit", { postedMessage: parentListItemID, fullTextContent: theText },
function(result){
if(result == 1) {
parentListItem.parents('ul').animate({ backgroundColor: 'blue' }, 500).animate({ backgroundColor: '#eeeeee' }, 500);
} else {
alert(result);
}
});
});
Here's the situation: I'm writing a simple AJAX application that performs CRUD functions. When the user double clicks on a particular element, the element changes into a text box so that they can edit inline. When the text box loses focus (code for which is below), the value of the textbox gets POSTed to a PHP script that updates the database.
All is groovy except for one thing. When I create a new record, which gets popped onto the top of the list with AJAX, I can't edit that record without refreshing the page. I mean, the edit looks like it's been committed, but when you refresh, it reverts back to the original. After refreshing, there are no issues.
To boil it down: When I try to run the following code on newly created rows in my table (both in the database and on the page), the edit appears to be made on the page, but never makes it to the database.
//Make changes on FOCUSOUT
$('#editable').live('focusout', function(){
var parentListItem = $(this).parents('li');
var theText = $(this).val();
var parentListItemID = parentListItem.parents('ul').attr('id');
$(this).remove();
parentListItem.html(theText);
parentListItem.removeClass('beingEdited');
$.post("databasefuncs.php?func=edit", { postedMessage: parentListItemID, fullTextContent: theText },
function(result){
if(result == 1) {
parentListItem.parents('ul').animate({ backgroundColor: 'blue' }, 500).animate({ backgroundColor: '#eeeeee' }, 500);
} else {
alert(result);
}
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想您没有将事件绑定到通过 AJAX 加载的新 DOM 元素。
I suppose you are not binding the event to the new DOM Element loaded via AJAX.
你的问题是 post 执行了,但你的目标函数 (func=edit) 永远不会触发,你的 php 永远不会读取你在问号之后发送的参数,你正在发送一个 post 请求并希望它的行为像 get通过将参数附加到 URL,将您的请求更改为:
现在在您的 PHP 中,您有 $_POST["func"] = "edit";
希望这很清楚并且有帮助。干杯。
Your problem is that the post executes but the function you target (func=edit) never fires, the params you are sending after the question mark are never read by your php, you are sending a post request and wanting it to behave like a get by attaching parameters to the URL, change your request to:
Now in your PHP you have $_POST["func"] = "edit";
Hope this is clear and it helps. Cheers.