简体   繁体   English

JavaScript files.length在ie9中不起作用需要替代方法

[英]JavaScript files.length not working in ie9 need alternative approach

I have the following JavaScript function which is failing in internet explorer 9 on the line which declares the variable filesattached . 我有以下JavaScript函数,在声明变量filesattached的行上的Internet Explorer 9中失败。

function VesselDetails() {      

    insurancestart = $('#ctl00_ContentPlaceHolder1_datetimepickerinsstart').val();
    insuranceend = $('#ctl00_ContentPlaceHolder1_datetimepickerinsend').val();
    filesattached = $("input:File")[0].files.length;  

    //set up JS objects
    $('#ctl00_ContentPlaceHolder1_datetimepickerinsend').datetimepicker({ format: 'd/m/Y H:i a' });
    $('#ctl00_ContentPlaceHolder1_datetimepickerinsstart').datetimepicker({ format: 'd/m/Y H:i a' });

    //subscribe to change events
    $('#ctl00_ContentPlaceHolder1_datetimepickerinsstart').change(function () {
        insurancestart = $("ctl00_ContentPlaceHolder1_datetimepickerinsstart").val();
    });

    $('#ctl00_ContentPlaceHolder1_datetimepickerinsend').change(function () {
        insuranceend = $("ctl00_ContentPlaceHolder1_datetimepickerinsend").val();
    });

    $("input:File").change(function () {
        filesattached = $("input:File")[0].files.length;
    });

    ins_client();
}

The ins_client method looks like this: ins_client方法如下所示:

function ins_client(sender, e) {
    if (pagemode == 'EditVessel') {
        e.IsValid = true;
    }

    if (pagemode == 'NewVessel') {
        if (insurancestart !== '' && insuranceend !== '' && filesattached > 0) {
            e.IsValid = true;
        }
        else {
            e.IsValid = false;
        }
    }
}

This all works perfectly well in chrome and ie 11 but the length property is returning an undefined for ie 9. I am using the length because I only want the page to be valid for a new vessel request once a document has been submitted, is there another way of doing this which will work in ie 9 onwards and chrome, apologies if this has already been answered elsewhere but I cannot find a workaround anywhere that enables this to continue working in the same way but in ie9 onwards and chrome. 这一切在chrome和ie 11中都可以很好地工作,但是length属性返回的是ie 9的undefined。我使用的是length,因为我只希望页面在提交文档后对新的容器请求有效,是否存在这样做的另一种方法将适用于9或更高版本和chrome,如果在其他地方已经回答过此问题,则表示歉意,但是我找不到在任何地方都可以使它以相同的方式继续工作的解决方法,但适用于9或更高版本和chrome。

I replaced: 我更换:

filesattached = $("input:File")[0].files.length;  

With: 附:

var areFilesAttached = document.getElementById('ctl00_ContentPlaceHolder1_fuAttachment').value ? true : false;

Within the VesselDetails function. VesselDetails函数中。

Then replaced the if statement within ins_client with the following: 然后将ins_clientif语句替换为以下内容:

if (pagemode == 'NewVessel') {
        if (insurancestart !== '' && insuranceend !== '' && areFilesAttached == true) {
            e.IsValid = true;
        }
        else {
            e.IsValid = false;
        }
    }

This was an alternative approach which enabled me to check whether or not a file had been provided without using the files.length property which is not compatible with IE9 . 这是一种替代方法,它使我能够在不使用与IE9不兼容的files.length属性的情况下检查是否已提供文件。

I'm afraid this can't be achieved, IE9 does not support HTML5 File API and therefore it returns undefined value for files property. 恐怕这无法实现,IE9不支持HTML5 File API,因此它会为files属性返回未定义的值。

Take a look at FILE API 看看FILE API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM