2012-06-15 162 views
0

这是我的第一个关于堆栈溢出的问题。我的javascript函数有问题。javascript浏览器兼容性问题

下面我我的js代码:

function abc(formobject){ 
    var ext =".txt"; 
     var abc=".doc,docx"; 
    if(abc.search(ext) < 0){ 
     alert("hi"); 
     formobject.file.value=""; 
     return false; 
    } 
    return true; 
} 

<input type="submit" id="button" value="submit" onclick="return abc(this.form);"> 

在这里提交按钮我打电话ABC(在单击)js函数并上传file.It执行逻辑,如果该文件是无效扩展它应该抛出警报并清除浏览按钮的值。它发生在Mozilla中,但IE8中的值没有得到清除。任何人都可以帮助我解决这个问题?

文件是哪个我在这里上传 感谢

+0

您可以发布代码找你文件的标签呢? –

+0

请参阅http://stackoverflow.com/questions/973661/ie8-causing-file-input-entry-to-be-blank-via-sendkeys 显然这是不可能出于安全原因。 – Itison

+0

cxyz

回答

0

而不是

formobject.file.value=""; 

尝试使用

formobject.reset(); 

希望这有助于。

0

我假设你在表单中只有“浏览”字段。

由于安全限制,无法访问文件输入标签的值字段。更多在此http://blogs.msdn.com/b/ie/archive/2008/07/02/ie8-security-part-v-comprehensive-protection.aspx

如果您只想清除文件字段,那么你必须在你的文件字段之前添加一个div标签并清除它。

例子: -

<html> 
<head> 
<script type="text/javascript"> 
function abc(formobject){  
var ext =".txt";   
var abc=".doc,docx";  
if(abc.search(ext) < 0){   
alert("hi");   
document.getElementById('uploadFile_div').innerHTML = 
        document.getElementById('uploadFile_div').innerHTML; 

return false;  
}  
return true; 
} 


</script> 
</head> 
<body> 
<form name="theForm"> 
<div id="uploadFile_div"> 
<INPUT NAME="file" TYPE="file"> 
</div> 
<input type="submit" id="button" value="submit" onclick="return abc(this.form);"> 

</body> 
+0

做同样的工作标签 – cxyz