TypeError:document.getElementById(...)。文件未定义

时间:2018-01-08 19:52:59

标签: javascript php jquery

function uppp(id){
    var eleme = "uploaded_image"+id;
    alert(id);
  var name = document.getElementById(id).files[0].name;
  var form_data = new FormData();
  var ext = name.split('.').pop().toLowerCase();
  if(jQuery.inArray(ext, ['gif','png','jpg','jpeg']) == -1) 
  {
   alert("Invalid Image File");
  }
  var oFReader = new FileReader();
  oFReader.readAsDataURL(document.getElementById(id).files[0]);
  var f = document.getElementById(id).files[0];
  var fsize = f.size||f.fileSize;
  if(fsize > 2000000)
  {
   alert("Image File Size is very big");
  }
  else
  {
   form_data.append("file", document.getElementById(id).files[0]);
   $.ajax({
    url:"http://localhost/wat/admin/category/uppp/"+id,
    method:"POST",
    data:form_data,
    contentType: false,
    cache: false,
    processData: false,
    beforeSend:function(){
     $('#uploaded_image').html("<label class='text-success'>Image Uploading...</label>");
    },   
    success:function(data)
    {
        var x = document.getElementById(eleme);
        x.style.color = 'green';
        x.innerHTML= "done";
    }
   });
  }

  }

此函数函数应该从输入控件获取文件名,然后将详细信息发布到php文件,该文件将在数据库中上传该名称。该功能正在“有选择地”工作......有时它有效,有时会产生上面的错误......这里是文件名所在的php文件...请帮忙

 while($row = mysqli_fetch_assoc($result)) {

                            $document_id= $row["document_id"];
                            $document_description = $row["document_description"];

                         $output .= "</tr>";

                         $output .= "<tr> ";

                          $output .= "  <td class='text-center'><p class='nums'>".$count."</p></td>
                            <td>
                                 <input type='text' name='' class='textdd' value= '".$document_description."' disabled>
                            </td>
                            <td word-wrap:break-word'>
                                 <input type='file' name='file' id='$document_id' size='20' required  />

                            </td>
                            <td>
                                 <button name='$document_id' onclick='uppp(this.name)'>Upload</button>
                            </td>
                            <td id='uploaded_image$document_id' color='red'></td>
                            ";

                    $output .= "</tr>";
                    $count++;



               }
                $output .= "<tr> <td> <input type = 'submit' value='Save Documents' class='btn btn-info'> </td> </tr> ";
                $output.= " </tbody> </table>";
                  echo $output;
                }
                else{

                    $output .="<td class= 'noresults'> NO documents Found </td>  </tr> ";
                     $output.= " </tbody> </table></form>";
                      echo $output; 

1 个答案:

答案 0 :(得分:0)

我意识到我在同一页面上有来自不同表格的元素具有相同的ID。这是因为从1到100动态创建id所以当函数传递一个id时,该id属于发生错误时的两个元素。

我意识到在为元素动态创建id时,创建过程应该尽可能唯一