未捕获的类型错误:不能读取属性'null - error在Chrome

Uncaught TypeError: Cannot read property 'value' of null - error in Chrome

本文关键字:null Chrome error 属性 读取 类型 错误 不能读 不能      更新时间:2023-09-26

html部分为:

<html:file property="claimUploadFile" id="claimUploadFile" size="50" />

Javascript部分是:

if(document.getElementById("claimUploadFile").value == ""){
            var messagePrompt = 'Some message';   
            alert(messagePrompt);
            return;
        }

以上代码在IE中工作正常,但在Chrome中给出错误"Uncaught TypeError: Cannot read property 'value' of null" .

我认为html:file是错误的,你的代码中也没有函数,所以你不能使用返回语句

    <form name="fileUpload">
       <input type="file" name="claimUploadFile" value="claimUploadFile" id="claimUploadFile">
    </form>

Js部分
   var file = document.forms['fileUpload']['claimUploadFile'].files[0];
     //file.name == "somepic.png"
     //file.type == "image/png"
     //file.size == 254845
    if(file == undefined){
        var messagePrompt = 'Some message';   
        alert(messagePrompt);
    }

document.getElementById('claimUploadFile').files[0]   // Does the same work

查看演示:https://jsbin.com/rodano/4/edit?html,js,console,output