我有以下 JavaScript 函数,但在 Internet Explorer 9 中声明变量 filesattached
的行中失败。
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();
}
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;
}
}
}
这一切在 chrome 和 ie 11 中工作得很好,但 length 属性对于 ie 9 返回未定义。我使用长度是因为我只希望页面在提交文档后对新容器请求有效,是否有另一种方法可以在 ie 9 及以上版本和 chrome 中工作,很抱歉,如果这已经在其他地方得到了回答,但我无法在任何地方找到解决方法,使其能够以相同的方式继续工作,但在 ie9 及以上版本和 chrome 中。
最佳答案
我替换了:
filesattached = $("input:File")[0].files.length;
与:
var areFilesAttached = document.getElementById('ctl00_ContentPlaceHolder1_fuAttachment').value ? true : false;
在VesselDetails
函数内。
然后将 ins_client
中的 if
语句替换为以下内容:
if (pagemode == 'NewVessel') {
if (insurancestart !== '' && insuranceend !== '' && areFilesAttached == true) {
e.IsValid = true;
}
else {
e.IsValid = false;
}
}
这是一种替代方法,使我能够检查是否已提供文件,而无需使用与 IE9
不兼容的 files.length
属性。
关于JavaScript files.length 在 ie9 中不起作用需要替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26996339/