2016-02-12 122 views
1

我试着让用户上传图片,并在图片上传完成,可以点击添加图片按钮,它会添加到文本框作为IMG SRC。虽然,它与[对象HTMLInputElement]返回类似于null,为什么会发生这种情况?<input type =“file”>与document.value()不起作用。

function added() { 
    var image = document.getElementById('fileToUpload'); 
    document.getElementById('media_post').value = '<img src="http://lit.life/gallry/<?php echo $dir_auth1; ?>/uploads/">' + image; 
} 

<form action="upload1.php" method="post" id="addedImage" enctype="multipart/form-data" data-ajax="false"> 
Select image to upload: 
<input type="file" name="fileToUpload" id="fileToUpload"> 
<input type="submit" value="Upload Image" name="submit"> 

<textarea rows="4" cols="45" id="media_post" name="media_post" form="usr_post" maxlength="300" method="get"> 

</textarea> 
+0

你已经在你的例子提供的代码似乎剂量不多大意义。 JS不应该在脚本标记中? –

+0

@DanWalker yup。它是。虽然,它实际上是在我的脚本中,只是没有在StackOverFlow – aidangig

回答

1

你想显示的<input>元素,而不是它的价值。

var image = document.getElementById('fileToUpload').value; 

如果你想这个文件名,成为图像URL的一部分,你需要把它src属性中。

document.getElementById('media_post').value = '<img src="http://lit.life/gallry/<?php echo $dir_auth1; ?>/uploads/' + image + '">' ; 

function added() { 
 
    var image = document.getElementById('fileToUpload').value; 
 
    document.getElementById('media_post').value = '<img src="http://lit.life/gallry/aidan/uploads/' + image + '">' ; 
 
}
Select image to upload: 
 
<input type="file" name="fileToUpload" id="fileToUpload" onchange="added()"> 
 
<input type="submit" value="Upload Image" name="submit"> 
 

 
<br>Preview 
 
<br> 
 
<textarea rows="4" cols="45" id="media_post" name="media_post" form="usr_post" maxlength="300" method="get"> 
 

 
</textarea>

+0

好吧,那么除了'.value'之外,我可以改变它,这对那些信息来说真的很好。 – aidangig

+0

'.value'将是'input'的文件名。你想让它在图像旁显示什么? – Barmar

+0

是的,这是我试图做的。把这个文件名输入到一个'img src'文本框中。 ' [对象HTMLInputElement]' 有何帮助? – aidangig

相关问题