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

Javascript 如何将文件名设置为输入值,javascript,Javascript,下面的脚本工作正常。。它在警报语句中显示文件名。。但我想要输入语句中的文件名。示例:如果我上载第一个文件,我希望文件名为id='0',如果它是第二个文件,我希望文件名为id='1' <body> <input type="file" class="input"/> <input type="file" class="input"/> <input type="file" class="input"/> <inp

下面的脚本工作正常。。它在警报语句中显示文件名。。但我想要输入语句中的文件名。示例:如果我上载第一个文件,我希望文件名为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>

var inputArray=document.getElementsByClassName('input');
for(变量i=0;i
选择新车时,会触发一个功能,输出所选车辆的值

函数myFunction(){ var x=document.getElementById(“mySelect”); var fileName=文件[x]。名称; document.getElementById(“0”).value=fileName; document.getElementById(“demo”).innerHTML=“您选择:”+x; }