2012-04-04 62 views
0

我正在嘗試將輸入類型隱藏到輸入類型文件中,onclick按鈕。該代碼在Mozilla Firefox和Internet Explorer上運行良好,但它不適用於Google Chrome瀏覽器和Safari瀏覽器。下面是我的代碼:更改輸入類型在Google Chrome瀏覽器和Safari瀏覽器上不起作用

<script type="text/javascript"> 
function hide() 
{ 
    $(document).ready(function() 
    { 
     $('#att').hide(); 
     $('#change').hide(); 
     $('#change1').hide(); 
     document.getElementById('fileatt').type='file'; 

    }) 
} 
<input type="text" name="att" value="<?php echo $name; ?>" id="att" disabled="disabled" size="12" style="float:left;"> 
<input name="fileatt" id="fileatt" type="hidden" size="12" style="float:left;"> 
<input type="button" name="browse" id="change" value="Browse" style="float:left;"> 
<input type="button" id="change1" value="Change" onclick="hide()"> 

禁用的文本框擁有一個數據庫創建的前值。並添加值爲「瀏覽」的按鈕,使其看起來像輸入類型文件。點擊更改按鈕後,我希望隱藏文本字段和瀏覽按鈕並顯示輸入類型文件。在Firefox和Internet Explorer上一切正常。但是我所面臨的問題是,在Chrome和Safari上,文本框和瀏覽按鈕成功消失,但輸入類型文件沒有出現。

回答

0

您不能更改inputtype,所以這是無效代碼:

document.getElementById('fileatt').type='file'; 

使用兩個元素,一個隱藏和顯示等,切換他們。

在這裏閱讀更多:
change type of input field with jQuery

+0

Thnkü這麼多gdoron ..你的鏈接對我非常有用.. – 2012-04-04 10:09:38

+1

Thnkü這麼多gdoron ..你的鏈接是非常有用的我.. 這裏就是我所做的: 我使用的輸入型「文件」,而不是'隱藏'爲id'fileatt'; 在窗口加載,我用jquery隱藏它使用.hide()函數和onclick我用.show()函數來顯示該字段。再次感謝你! – 2012-04-04 10:17:33

0

試着這麼做:

function hide() 
    { 
     $(document).ready(function() 
     { 
      $('#att').hide(); 
      $('#change').hide(); 
      $('#change1').hide(); 
      $('#fileatt').attr('type', 'file');    //document.getElementById('fileatt').type='file'; 

     }) 
    } 
+0

閱讀[我的答案](http://stackoverflow.com/a/10008658/601179),你不能改變一個輸入類型與jQuery或在IE中! – gdoron 2012-04-04 09:48:26

0

試試這個

$('#fileatt').attr('type', 'file'); 

代替

document.getElementById('fileatt').type='file'; 
+0

閱讀[我的答案](http://stackoverflow.com/a/10008658/601179),你不能改變一個輸入類型與jQuery或在IE中! – gdoron 2012-04-04 09:52:51

相關問題