2017-01-30 67 views
0

这是我想要实现的一件小事。我有一个asp.net FileUpload和一个文本框。当用户单击fileUpload并从他/她的计算机/设备中选择一张图片时,我希望图片名称在提交前立即显示在文本框中。这是我已经试过如何将文件名显示为只读文本框

<asp:FileUpload ID="Upload" runat="server" ClientIDMode="Static" /> 
<asp:TextBox ID="txtImage" runat="server" ClientIDMode="Static"> 

$('#Upload').change(function() { 

       var filename = $(this).val(); 
       var lastIndex = filename.lastIndexOf("\\"); 
       if (lastIndex > 0) { 
        filename = filename.substring(lastIndex + 1); 
       } 
       $('txtImage').val(filename); 
      }); 

它仍然不能让它显示。我希望我可以请

+3

$(“txtImage”) - 缺少#或。?编号还是班级? $( “#txtImage”)。同时检查是否有任何控制台错误。 – Terrance00

+0

你的评论确实帮了我。我添加了#解决它的一部分。另外,有我的jQuery参考问题。这已经被整理出来了。它运作良好。谢谢 – Mcbaloo

回答

0

你缺少#$("txtImage")。这应该是这样的:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      console.log("ready!"); 
      $('#Upload').change(function() { 

       var filename = $(this).val(); 
       var lastIndex = filename.lastIndexOf("\\"); 
       if (lastIndex > 0) { 
        filename = filename.substring(lastIndex + 1); 
       } 
       $('#txtImage').val(filename); 
      }); 
     }); 

    </script> 

<asp:FileUpload ID="Upload" runat="server" ClientIDMode="Static" /> 
<asp:TextBox ID="txtImage" runat="server" ClientIDMode="Static"></asp:TextBox> 
0

TextBox txtImage没有结束标记。

<asp:TextBox ID="txtImage" runat="server" ClientIDMode="Static"/>