2017-01-30 119 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"/>