2012-04-04 61 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

相关问题