如何将文件名设置为输入值

时间:2016-02-11 16:09:34

标签: javascript

下面的脚本工作正常..它在alert语句中显示文件名..但我想在输入语句中使用filename。例如,如果我在id =' 0'上传我希望文件名的第一个文件。 ,如果是第二个,我想在id =' 1'中找到文件名。

<body>
    <input type="file" class="input"/>
    <input type="file" class="input"/>
    <input type="file" class="input"/>
    <input type="file" class="input"/>
    <input type="file" class="input"/>
    <input id="0">
    <input id="1">
    <input id="2">
    <input id="3">
    <input id="4">
    <script>
    var inputArray = document.getElementsByClassName('input');

    for(var i = 0; i < inputArray.length; i++){
        inputArray[i].addEventListener('change',prepareUpload,false);
    };

    function prepareUpload(event)
    {
        var files = event.target.files;
        var fileName = files[0].name;
        document.getElementById("0").value = fileName;
        alert(fileName);
    }
    </script>
    </body>










<!DOCTYPE html>
<html>
<body>

<p>Select a new car from the list.</p>

<input id="mySelect" onchange="myFunction1()" type="file">


<p>When you select a new car, a function is triggered which outputs the value of the selected car.</p>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("mySelect");
    var fileName = files[x].name;
    document.getElementById("0").value = fileName;
    document.getElementById("demo").innerHTML = "You selected: " + x;
}
</script>

</body>
</html>

0 个答案:

没有答案