文件上传获胜';t触发以下循环(第一个循环除外)


File Upload won't trigger on the following loop except the first one

我正在为我的Web应用程序使用Twitter模式引导。该应用程序的一个功能是显示一个包含相应信息的表(我使用mysql_query填充信息)。对于表格的每一行,都有一个"查看"按钮。单击"查看"按钮将打开一个"模式"弹出窗口,其中包含一个表单。正如你所看到的,我正在尝试使用AJAX上传一个文件。当我试着在第一排做这件事时,这件事就成功了。但是,当我尝试在下面一行进行操作时,我无法上传文件,就好像文件中没有表单一样

表:

<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'>&times;</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>

Javascript:

$('#uploadPicture').click( function(e) {
    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();
});

上传.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;
}
}
}
?>

提前感谢!:)

不要使用'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();
});

这就行了。如果你想接受我的建议,那么我会说在"style"属性中使用ID真的是个坏主意。我会说像这个一样使用它

<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();
  });

不使用$('#uploadPicture').click( function(e) { ...使用$('#uploadPicture').on('click', function(e) {...