Javascript 文件上传赢得';除第一个循环外,在以下循环上不触发
我正在为我的Web应用程序使用Twitter模式引导。该应用程序的一个特点是显示一个包含相应信息的表(我使用mysql\u查询填充信息)。对于表格的每一行,都有一个“查看”按钮。单击该视图按钮将打开一个“模态”弹出窗口,其中包含一个表单。如您所见,我正在尝试使用AJAX上传一个文件。当我在第一行尝试这样做时,这是成功的。但是,当我尝试在下一行执行此操作时,我无法上传文件,就好像文件中没有表单一样 表:Javascript 文件上传赢得';除第一个循环外,在以下循环上不触发,javascript,php,ajax,Javascript,Php,Ajax,我正在为我的Web应用程序使用Twitter模式引导。该应用程序的一个特点是显示一个包含相应信息的表(我使用mysql\u查询填充信息)。对于表格的每一行,都有一个“查看”按钮。单击该视图按钮将打开一个“模态”弹出窗口,其中包含一个表单。如您所见,我正在尝试使用AJAX上传一个文件。当我在第一行尝试这样做时,这是成功的。但是,当我尝试在下一行执行此操作时,我无法上传文件,就好像文件中没有表单一样 表: <table border = '1' width = "30%"> &l
<table border = '1' width = "30%">
<tr>
<th>Asset Picture</th>
<th>Asset Name</th>
<th>Action</th>
</tr>
<?php
$data=mysql_query("SELECT * FROM assets") or die("No records found.".mysql_error());
while($row = mysql_fetch_array($data))
{
echo "<tr>
<td><img src = '$row[asset_Picture]' width = '100%'></td>
<td>$row[asset_Name]></td>
<td><button data-toggle='modal' id = 'buttonView' style = '$row[asset_ID]' data-target='#myModalEdit-$row['asset_ID']' type='button' class='btn btn-link'>View</button></td>
</tr>";
echo "<div class='modal fade' id='myModalEdit-$row['asset_ID']' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>"; ?>
<div class='modal-dialog'>
<div class='modal-content'>
<div class='modal-header'>
<button type='button' class='close' data-dismiss='modal' aria-hidden='true'>×</button>
<h4 class='modal-title' id='myModalLabel'><b>Testing</b></h4>
</div>
<div class='modal-body'>
<?php
echo "<form style = '$row[asset_ID]' id='uploadForm-$row[asset_ID]' action = 'upload.php' method = 'POST'>
<center><img value = '$row[asset_Picture]' id = asset_Picture-$row[asset_ID]' class = 'img-thumbnail' src = '$row[asset_Picture]' width = '60%'></center><br><br>
<b class = 'text-danger'>Change Asset Picture?</b><br><br>
<input class = 'form-controlModal' type = 'file' name = 'userImage' id = 'file-$row[asset_ID]' accept='image/x-png, image/gif, image/jpeg, image/pjpeg, image/jpg, image/png' style = 'width:460px;'>
<input type='submit' id = 'uploadPicture' style = '$row[asset_ID]' class='btn btn-primary' value = 'Upload'>
</form>";
?>
</div>
<div align = 'left' class='modal-footer'>
<button type='button' class='btn btn-warning' data-dismiss='modal'>Close</button>
</div>
</div>
</div>
</div>
<?php
}
?>
</table>
upload.php
<?php
if(is_array($_FILES)) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
$sourcePath = $_FILES['userImage']['tmp_name'];
$targetPath = "upload/".$_FILES['userImage']['name'];
if(move_uploaded_file($sourcePath,$targetPath)) {
echo $targetPath;
}
}
}
?>
提前感谢!:) 而不是使用$('#uploadPicture')。单击(函数(e){…
使用
$('#uploadPicture')。在('click',函数(e){…
上,而不是使用'id'使用'class'。使用'id'绑定仅对第一个元素有效。偶数仅绑定到命中该元素的第一个id。请使用
<input type='submit' style = '$row[asset_ID]' class='uploadPicture btn btn-primary' value = 'Upload'>
现在像这样修改脚本
$('.uploadPicture').click( function(e) { // <-- starting with '.' means for all objects of this class
var edit_id = $(this).attr("style");
$.ajax({
url: "upload.php",
type: "POST",
data: new FormData($("form#uploadForm-"+edit_id)[0]),
contentType: false,
cache: false,
processData:false,
success: function(data)
{
alert("success!");
},
error: function()
{
}
});
e.preventDefault();
});
<input type='submit' id = '$row[asset_ID]' class='uploadPicture btn btn-primary' value = 'Upload'>
$('.uploadPicture')。单击(函数(e){//我尝试过,但问题仍然存在。我可以在第一条记录上上载文件。但是当我尝试在以下行(第二行、第三行等)上上载时,整个ajax都不起作用。顺便说一句,我感谢你的帮助。这就像下面的行没有a,因为我只能在第一条记录上上传。你知道如何在这个场景中唯一定义a吗?你不能对多张图片使用相同的id。这篇文章让我非常高兴。非常感谢你,先生!我学到了新的技巧和这个代码修订版完成了我的整个项目!谢谢你,先生!!!!总是很乐意帮助:)嗯,我有一个问题。现在,无论行是什么,AJAX都能完美地工作,这是一件好事,但是当我尝试上传到以下行时,这个错误“未识别索引:userImage”不断出现。我想我对这个“新表单数据($)有问题表单#uploadForm-“+edit_id)[0])”。由于我无法在这里测试php,我无法确定,但我认为问题在于表单没有设置“enctype”属性。将该属性设置为
<input type='submit' id = '$row[asset_ID]' class='uploadPicture btn btn-primary' value = 'Upload'>
$('.uploadPicture').click( function(e) {
var edit_id = $(this).attr("id"); // <------ pick from id
$.ajax({
url: "upload.php",
type: "POST",
data: new FormData($("form#uploadForm-"+edit_id)[0]),
contentType: false,
cache: false,
processData:false,
success: function(data)
{
alert("success!");
},
error: function()
{
}
});
e.preventDefault();
});