使用 IE8 未触发 Jquery 事件

Jquery event not being fired with IE8

本文关键字:Jquery 事件 IE8 使用      更新时间:2023-09-26

我正在使用blueimp的文件上传javascript小部件和jQuery 1.9.0将文件上传到服务器,并且在IE8中有一个事件未触发,这是我的项目所需的浏览器之一。

因此,我永远无法打开窗口来浏览文件。如果我从IE8控制台执行$('#fileupload').click(),它可以工作,但我的代码没有成功。

我认为这是因为事件的冒泡而发生的,但我正在向您展示自己决定的代码。我还认为样式对这个问题很重要,因为我在实际文件上传小部件上使用的样式按钮可能必须重叠它。也许像不透明度和位置这样的 css 样式很重要。

目录:

<form id="supportForm">
    <div class="formTable">
        <div class="formRow">
            <label>User:</label>
            <input name="user" type="text" class="form-control validate[required]" placeholder="John Smith">
        </div>
        <div class="formRow">
            <label>Phone:</label>
            <input name="phone" type="tel" class="form-control validate[required]" placeholder="Ej: 881243989">
        </div>
        <!-- ... -->
        <!-- Some inputs and stuff -->
        <!-- ... -->
        <div class="formRow">
            <label>Attached files:</label>
            <div class="formCell">
                <span class="btn btn-default fileinput-button">
                    <span>Browse</span>
                    <input id="fileupload" type="file" name="files[]" data-url="../server/upload.html" class="submitFilesButton" multiple="">
                </span>
            </div>
        </div>
        <div class="formRow">
            <label></label>
            <div id="additional" class="formCell">
            </div>
        </div>
        <div class="formRow">
            <label></label>
            <div class="formCell">
                <button id="remedyButton" type="submit" class="btn btn-primary">Send</button>
                <button id="resetButton" type="reset" class="btn btn-default">Clean</button>
            </div>
        </div>
    </div>
</form>

Javascript:

$(document).ready(function() {
    upload_comp = $('#fileupload')
            .fileupload({ // Widget options
                singleFileUploads : false,
                autoUpload : false,
                dataType : 'json'
            })
            .on("fileuploadadd",
                function(e, data) { // Function for adding files after opening the browse window
                    if (data.files.length > 0) {
                        $('input[type=file]').css('color', 'transparent');
                    }
                    // Creating divs for the selected files in the previous browse window
                    for ( var i = 0; i < data.files.length; i++) {
                        var name = $('<div></div>').text(data.files[i].name);
                        var icon = $('<span class="fa fa-2x fa-trash" data-toggle="tooltip" data-placement="bottom" title="Delete">');
                        icon.click(function() {
                            // This handler creates a button for removing the actual file
                            for ( var k = 0; k < fileList.length; k++) {
                                if (fileList[k].name == $(this).parent().text()) {
                                    fileList.splice(k, 1);
                                    break;
                                }
                            }
                            $(this).parent().remove();
                        });
                        fileDiv = $('<div class="item">').wrapInner(name).append(icon);
                        fileDiv.appendTo($('#additional'));
                        fileList.push(data.files[i]);
                    }
                    //createTooltip($('.fa-trash'));
                });
    if (isNavigator(CONSTANTS.BROWSER.IE8)) {
        // Maybe could use something here and force the click()
        // But my current solution creates an infinite loop
    }
});

Css:

/* These are specifically for IE8 and are located in ie8.css */
DIV.formRow > * {
    width: auto;
}
INPUT[type=file] {
    display : none;
}
/* These are for all browsers in another css file, also for IE8 when there is no rule in the previous file */
.formTable {
    border-spacing:1em;
    display: table;
    width: 45em;
    min-width:45em;
    margin: 0 auto;
}
div.formRow {
    display: table-row;
}
div.formRow > label, #additional {
    width: 35%;
}
div.formRow > * {
    display: table-cell;
    width: 100%;
}
div.formCell {
    display: table-cell;
    width: 100%;
}
#fileupload {
    float:left;
    top: 0;
    right: 0;
    left: 0;
    margin: 0;
    position: absolute;
    width: 100%;
    height: 100%;
    padding: initial;
}
.item {
    border: 1px;
    border-color: black;
    border-style: solid;
    border-radius: 5px;
    border-width: thin;
    display: inline-block;
    padding: 4px;
    margin:0.25em;
}
.item div {
    vertical-align:text-bottom;
    display: inline;
}
.fa-trash {
    margin-left: 1em;
    cursor:pointer;
    color: #428BCA;
}
#company {
    display: inline-block;
    width: 49%;
}
#warehouse {
    float:right;
    width: 49%;
    display: inline-block;
}
.submitFilesButton {
    opacity:0;
    position:absolute;
}
.fileinput-button {
    text-align:center;
    white-space: nowrap;
    position: relative;     
}
#resetButton, #remedyButton {
    margin: 0.4em;
    float:right;
}
label {
    vertical-align:top;
}

这已经开放了足够长的时间。看起来@Sampson是对的,IE8不支持文件的JS API。