使用ajax和jquery发送数据-textarea.live('blur') [英] Sending data with ajax and jquery - textarea.live('blur')
问题描述
我想创建脚本来编辑图库中的照片.我有文本框可插入照片标题,在其中插入标题后,将其保留在数据库中. 当我仅更改一张照片的标题,但是当我更改更多标题然后重新加载页面时,所有更改的照片都具有相同的标题(最后插入),它可以正常工作. 有人可以帮我解决什么问题吗?
I want to create script to edit photos in galery. I have there textbox to insert title of photo and after insert title in it and leave textbox it'll update in database. It works ok, when I change title by only one photo, but when I change more titles and then I reload page, all changed photos have the same title (which was insert last). Can somebody help me please what is wrong?
我现在正在使用的代码是
There is code which I using now:
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 name='title' id='title' onchange='UpdateTitle($idPhoto);' rows='2' cols='22'>$title</textarea>
推荐答案
更多有用的编程技巧,而不是回答问题,因为它已经被回答了,但这会更干净:
More of a good programming tip rather than answering the question since it has already been answered but this would be cleaner:
$('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:
<textarea name='title' id='title' data-id='$idPhoto' rows='2' cols='22'>$title</textarea>
将工作得更好,并且不会造成混乱.
Will work much better and will be less confusing.
这篇关于使用ajax和jquery发送数据-textarea.live('blur')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!