kkynigu1984
2015-12-12 13:25
采纳率: 100%
浏览 3.3k

js中onchange事件不能调用函数

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
    function my_check(filename){
    window.alert("ok");
    var index = filename.lastIndexOf(".");
    var ext = filename.substr(index);
    if(ext == ".jpg" || ext == ".png" || ext == ".gif"){
        var obj = document.getElementById("form");
        obj.innerHTML("<input type = "button" value = "上传" />");
    }
    else{
        document.write("非法图片类型!");
    }
}
</script>
</head>

<body>
<form id="form" method="post" enctype="multipart/form-data">
<input type="file" name="uploadFile" onchange="javascript:my_check(this.value);"/>
</form>
</body>
</html>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • rex0654335 2015-12-12 13:54
    已采纳

    function my_check(filename){
    window.alert("ok"); var index = filename.lastIndexOf(".");
    var ext = filename.substr(index);
    if(ext == ".jpg" || ext == ".png" || ext == ".gif"){
    var obj = document.getElementById("form");
    obj.innerHTML("");
    }
    else{
    document.write("非法图片类型!");
    }

    }
    
    打赏 评论
  • Royal_lr 2015-12-12 15:25

    这里并没看到onchange事件

    打赏 评论
  •      function my_check(filename){
        window.alert("ok");
        var index = filename.lastIndexOf(".");
        var ext = filename.substr(index).toLowerCase();alert(ext)
        if(ext == ".jpg" || ext == ".png" || ext == ".gif"){
            var obj = document.getElementById("form");
            obj.innerHTML=('<input type = "button" value = "上传" />');//innerHTML是属性,不是方法,而且字符串也不闭合
        }
        else{
            alert("非法图片类型!");//不要document.write,会覆盖当期页面内容
        }
    }
    
    打赏 评论

相关推荐 更多相似问题