2012-05-11 156 views
0

代碼的想法是製作一個單獨的文本框和按鈕,所以我可以爲每個創建類,並使它們像fileupload一樣工作。Javascript not working(textbox.value = fileupload.value)

但是,JavaScript運行兩次,並且fileupload值被擦除。

<style type="text/css"> 
div.fileinputs 
{ 
    position: relative; 
} 

div.fakefile 
{ 
    position: absolute; 
    top: 0px; 
    left: 0px; 
    z-index: 1; 
} 

input.file 
{ 
    visibility:hidden; 
} 
</style> 
<script type="text/javascript" language="javascript"> 
    function uploadFileChange() { 
      alert("start"); 
      document.getElementById('FileName').value = document.getElementById("FileUpload1").value; 
      alert("end"); 
    } 
</script> 
<div class="fileinputs" style="width: 50%;"> 
    <!-- Upload file invisivel--> 
    <asp:FileUpload ID="FileUpload1" ClientIDMode="Static" class="file" runat="server" 
     onpropertychange="uploadFileChange();" /> 
    <!-- button e textbox falsas para poder dar syles ao button--> 
    <div class="fakefile"> 
     <asp:TextBox ID="FileName" CssClass="textbox" ClientIDMode="Static" runat="server" Width="31%" ></asp:TextBox> 
     <asp:Button ID="FileChooserButton1" CssClass="btnSubmit" runat="server" Text="Procurar..." 
      ForeColor="White" onClick="document.getElementeByID('FileUpload1').click()" /> 
    </div> 
</div> 

什麼問題??? 在此先感謝

回答

1

一點修正你的代碼,

<asp:Button ID="FileChooserButton1" CssClass="btnSubmit" runat="server" Text="Procurar..."    ForeColor="White" onClientClick="document.getElementeByID('FileUpload1').click();return false;" /> 

寫作返回false,clickevent不會回來後,爲什麼你失去fileuplaod值是因爲該網頁後得到支持的原因。

,否則你可以試試這個,

<asp:Button ID="FileChooserButton1" CssClass="btnSubmit" runat="server" Text="Procurar..." ForeColor="White" onClientClick="CallClick();return false;" /> 

function CallClick(){ 
document.getElementeByID('FileUpload1').click(); 
return false; 
} 
+0

感謝你洙多 –

+0

@JoaoSilva歡迎你:)編碼快樂.. –