【问题标题】:SCRIPT5007: Unable to get value of the property '0': object is null or undefinedSCRIPT5007:无法获取属性“0”的值:对象为空或未定义
【发布时间】:2014-02-03 12:36:54
【问题描述】:

我在 portlet 中上传图片并检查了图片的大小 - 如果大于 5MB,它应该显示警报,但在 IE(确切地说是 IE9)中它没有显示警报,这会引发错误 -

SCRIPT5007:无法获取属性“0”的值:对象为空或 未定义

代码

<script type="text/javascript" >

function _ImageUploadService_WAR_ImageUploadServiceportlet_initEditor() {
//if you dont wnat the default text you can remove.

return '';
}

function submitForm() {

if( $("#_ImageUploadService_WAR_ImageUploadServiceportlet_Picture").val() == ""){
    alert("Please check, You have not Filled Required Field!!");
}else{
        var result = confirm("Are you Sure?");

        if (result == true) {
                document._ImageUploadService_WAR_ImageUploadServiceportlet_photoGallery.submit();
        } else {
            return false;
        }
}
}

function checkSize(e){
var val = document.getElementById(e);
var sizeInMB = (val.files[0].size / (1024*1024)).toFixed(2);
if(sizeInMB > 5){
    alert("Attachment Size Exceeds The Allowable Limit!!");
    $("#"+e).val("");
}
}

</script>

【问题讨论】:

  • 错误的行号是多少?
  • 抱歉忘记提了。错误在 line-var sizeInMB = (val.files[0].size / (1024*1024)).toFixed(2);

标签: javascript internet-explorer-9 liferay-6 portal


【解决方案1】:

问题在于 Internet Explorer 9 及更低版本不支持File API。如果 File API 不可用,您可能应该跳过。

这样试试:

if(val.files) {
    var sizeInMB = (val.files[0].size / (1024*1024)).toFixed(2);

    if(sizeInMB > 5){
        alert("Attachment Size Exceeds The Allowable Limit!!");
        $("#"+e).val("");
    }
}

【讨论】:

  • 您的val 似乎是null 或undefined。
  • 不,它不为空。我发出警报来检查 val 的值,它在警报中显示 [objectHTMLInputElement]。
  • 登录 val.files 以查看它是否已定义。像这样console.log(val.files);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-24
  • 2011-08-12
  • 1970-01-01
  • 1970-01-01
  • 2013-08-10
  • 2014-12-10
相关资源
最近更新 更多