简体   繁体   English

ASP MVC剃刀将输入文件的值存储在文本框中

[英]Asp mvc razor store value of input file in textbox

I have the following code in my view, I need to store name of selected file from the input file in the textbox above, I have tried the following JS script but I get no results 我在视图中有以下代码,我需要在上面的文本框中存储从输入文件中选择的文件的名称,我尝试了以下JS脚本,但未得到任何结果

@Html.TextBoxFor(model => Model.helpVM.str1, htmlAttributes:new { id="inputId", @class = "form-control" })

<div class="col-md-4">
    <a href="#" onclick="document.getElementById('fileID').onchage(); return false;" class="btn btn-info">Choisir fichier</a>
     <input type="file" name="FileUpload" accept=".txt" data-input="false"  id="fileID" style="visibility: hidden;" />
 </div>  


            <script type="text/javascript">
                var filename = $('#fileID').val();
                document.getElementById("fileID").onchange = function () {
                    document.getElementById("inputId").value = filename
                };

In fire bug i get this error TypeError: document.getElementById(...).onchange is not a function 在火灾中,我收到此错误TypeError:document.getElementById(...)。onchange不是一个函数

<script type="text/javascript">
    document.getElementById("fileID").onchange = function () {
        document.getElementById("inputId").value= this.value;
    }     
</script>

you should use Change event, not click. 您应该使用Change事件,而不是单击。 and the syntax should be like this: 语法应该是这样的:

document.getElementById("fileID").onchange = function () {
    document.getElementById("inputId").value = this.value;
}

update: 更新:

well I suggest you to change your code as below : 好吧,我建议您如下更改代码:

<div class="col-md-4">
    <a href="#" onclick="HandleIt();" class="btn btn-info">Choisir fichier</a>
    <input type="file" name="FileUpload" accept=".txt" data-input="false"  id="fileID" style="visibility: hidden;" />
</div>  


<script type="text/javascript">
    function handleIt(){            
        document.getElementById("fileID").onchange = function () {
            document.getElementById("inputId").value = this.value;
        };
        return false;
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM