使用ajax和jquery发送数据 – textarea.live(’blur’)
我想创建脚本来编辑galery中的照片。 我有文本框插入照片的标题,插入标题后,留下文本框,它将在数据库中更新。 它工作正常,当我只通过一张照片更改标题时,但当我更改更多标题然后我重新加载页面时,所有更改的照片都具有相同的标题(最后插入)。 请问有人可以帮助我吗?
我现在使用的代码:
function UpdateTitle(idPhoto) { var id = idPhoto; $(document).ready(function(){ $('textarea').live('blur',function () { var titleVal = $(this).val(); $.ajax({ type: "POST", url: "changeTitle.php", data: {title:titleVal , id:id}, success: function(msg) { $('.'+id).html(msg); } }) }); }); }
更多的是一个好的编程提示,而不是回答这个问题,因为它已经得到了回答,但这会更清晰:
$('textarea').on('blur',function () { var titleVal = $(this).val(), id = $(this).data('id'); $.ajax({ type: "POST", url: "changeTitle.php", data: {title:titleVal , id:id}, success: function(msg) { $('#'+id).html(msg); } }) });
使用此HTML:
会更好地工作,而且会减少混乱。
使用$('#'+id)
代替$('.'+id)
并使用val代替html
来填充textarea。
所以基本上你应该有
$('#'+id).val(msg);
(假设msg直接是预期的内容)
但现在已经弃用了。 建议你使用on ,如下所示:
$(document).on('blur', 'textarea',function () { // something more precise than "document" would be better