Jquery IE8 未触发事件
Jquery event not being fired with IE8
我正在使用 blueimp 的文件上传 javascript 小部件和 jQuery 1.9.0 将文件上传到服务器,我有一个事件没有在 IE8 中触发,IE8 是必需的浏览器之一对于我的项目。
因此,我永远无法打开 window 来浏览文件。如果我从 IE8 控制台执行 $('#fileupload').click() ,它可以工作,但我在代码中没有成功。
我认为这是由于事件的冒泡而发生的,但我向您展示了自行决定的代码。我还认为样式对于这个问题很重要,因为也许我在真实文件上传小部件上使用的样式按钮必须与它重叠。也许 css 不透明度和位置等样式很重要。
Html:
<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。
我正在使用 blueimp 的文件上传 javascript 小部件和 jQuery 1.9.0 将文件上传到服务器,我有一个事件没有在 IE8 中触发,IE8 是必需的浏览器之一对于我的项目。
因此,我永远无法打开 window 来浏览文件。如果我从 IE8 控制台执行 $('#fileupload').click() ,它可以工作,但我在代码中没有成功。
我认为这是由于事件的冒泡而发生的,但我向您展示了自行决定的代码。我还认为样式对于这个问题很重要,因为也许我在真实文件上传小部件上使用的样式按钮必须与它重叠。也许 css 不透明度和位置等样式很重要。
Html:
<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。