如何使用相同的id但不同的内容进行更新
How can I do update with using same id but different content?
目前,我正在处理一个为图像文件输入的表单。浏览完图片后上传,我会得到图片的id。这是我的POST代码。
$("#smallpicture_id").change(function () {
displayAndShowImage(this,'#smallimg','#smallimg');
});
$("#largepicture_id").change(function () {
displayAndShowImage(this,'#largeimg','#largeimg');
});
function displayAndShowImage(input,targetHtmlElementName) {
if (input.files && input.files[0]) {
var files = input.files;
var reader = new FileReader();
reader.onload = function (e) {
$(targetHtmlElementName).attr('src', 'images/uploading.gif');
var formData = new FormData();
formData.append('userfile',files[0],files[0].name);
createImage(
config,
formData,
{
onSuccess : function(data) {
$(targetHtmlElementName).attr('src', e.target.result);
$.cookie(input.id, data);
console.log("Image has been save - Received ID: " + data + " saved in the cookie " + input.id);
},
onError : function(jqXHR, status) {
$(targetHtmlElementName).attr('src', 'images/img-error.png');
console.log("ERROR " + jqXHR.responseText + "'r'nstatus = " + status);
}
}
);
}
reader.readAsDataURL(files[0]);
}
}
Ajax
function createImage(cfg,formData,callbacks) {
var xhr = new XMLHttpRequest();
xhr.open('POST', cfg.url + "/image/", true);
xhr.onload = function () {
if (xhr.status === 200) {
// File(s) uploaded.
callbacks.onSuccess(xhr.responseText.trim());
} else {
callbacks.onError(xhr);
}
};
xhr.send(formData);
}
我的问题是,如何使用与图像相同的id更新/删除图像。我已经可以进行POST和GET,但我仍然不知道如何更新和删除。
您可以在FormData
查询标识符和ID中附加两个字符串(仅在更新和删除的情况下),如
formData.append('queryType', 'DELETE')
formData.append('imageID', input.id)
在服务器端代码(你已经添加了保存新图像的代码)上,你必须添加类似的条件
<?php
$identifier=$_POST['queryType'];
if($identifier=="NEW") {
//save file with new ID and return ID
} elseif ($identifier=="UPDATE")
//update Image Data ($_FILE) with ID appended in formdata
} elseif ($identifier=="DELETE")
//Delete existing image at ID specified
}
?>
希望这能有所帮助。
您可以为每个上传进程指定具有相同id的元素特定类名,然后运行displayAndShowImage函数,只为具有"update this"类名的元素指定类名。
$("#smallpicture_id").change(function () {
$(this).addClass("update-this"); // add update-this class
$(".update-this").not($(this)).removeClass("update-this"); // remove all update-this classnames from all other ones
// then run your function for only element which has update-this classname
displayAndShowImage(this,'.update-this');
});
相关文章:
- $rootScope未使用forEach进行更新
- 使用AngularJS中的筛选器更新给定的表
- 使用mongodb更新中的一个变量
- 无法在现有状态转换期间更新-未使用任何非法的setState()
- 使用它更新集合中的嵌套数组's索引
- 使用Javascript更新输入框中的文本
- 如果我不应该在组件WillUpdate中调用setState,如何更新状态(使用ReactJS)
- Angular.js:如何将数据从一个HTML更新到使用相同控制器的另一个HTML
- 在 Twitter Bootstrap 3.0 中动态设置弹出框值,并在滚动更新时使用 href
- Javascript setInterval没有't更新内部使用的时间戳
- 只更新已使用Webpack.watch()更改的区块
- 中继器更新时使用动画
- Ajax X和Y位置更新无法使用Jquery UI可拖动程序正常工作
- 推迟或暂停敲除中的依赖项评估,直到视图模型完全更新(例如使用映射插件)
- 作用域是't更新$apply使用
- 在应用更新时使用本地存储进行应用内购买
- 内联更新mysql使用jquery/php
- AngularJS -变量在视图中不更新(不使用作用域)
- 如何更新“复杂”使用jQuery's .data()存储的数据
- 无缓存和更新在使用JSP和Javascript的IE 10中不起作用