簡體   English   中英

文件輸入我做錯了什么?

[英]File input what am I doing wrong?

我一直在研究這個表單一段時間試圖獲取一個文本字段來模仿文件輸入字段(點擊它時觸發隱藏文件字段打開)。 我使用了一些Javascript來更改文本字段的VALUE,但我無法讓它工作。 在這里查看http://jsfiddle.net/ygMLm/

<script type="text/javascript">
 function getFile(){
   document.getElementById("uploaded").click();
 }
 function sub(obj){
    var file = obj.value;
    var fileName = file.split("\\");
    document.getElementById("upclicked").value = fileName[fileName.length-1];
    document.adupload.submit();
    event.preventDefault();
  }
</script>

<p><input type="text" id="upclicked" onclick="getFile()" placeholder="Select a File (960 Width JPEG)"></p>
<div style='height: 0px;width: 0px; overflow:hidden;'>
<input type="file" name="date" value="" id="uploaded" onclick="getFile()">

編輯:

$('#uploaded').change(function(){
    var file = obj.value;
    var fileName = file.split("\\");

    $('input[name=faux]').val(fileName[fileName.length-1]);
});

將此添加到您的jQuery代碼以在DOM就緒上運行:

$("#uploaded").change(
  function() {
    var fullPath = $(this).val();
    var splitPath = fullPath.split("\\");
    $("#upclicked").val(splitPath[splitPath.length - 1]);
  }
);

看下面的帖子

如何在文件選擇上觸發事件

有關如何在file控件上使用change()事件的示例。 您應該放置代碼以更新此事件內的文本框的值。

希望有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM