点击隐藏文件输入jQuery错误
Click hidden file input jQuery error
我试图点击一个隐藏的输入file
。我已经在这里测试过了,它工作得很好。
在实际项目中,我得到这个错误Uncaught RangeError: Maximum call stack size exceeded
。
这是我的:
HTML:<div id="upload-image-container" class="upload-image-container inline-block">
<h2 class="inline-block">+ Upload Your Photo!</h2>
<input type="file" name="images" id="images" />
</div>
jQuery: jQuery("#upload-image-container").click(function() {
// e.preventDefault();
jQuery("input[type='file']").click();
});
我已经尝试添加e.preventDefult
或e.preventImmediatePropagation
,但我仍然得到相同的错误。
我真不知道怎样才能解决这个问题。
我在file
输入上有一个监听器…图片自动上传。不确定这是不是导致问题的原因。我试着把它去掉,结果还是一样。
input.addEventListener("change", function (evt) {
var i = 0, len = this.files.length, img, reader, file;
for ( ; i < len; i++ ) {
file = this.files[i];
if (!!file.type.match(/image.*/)) {
if ( window.FileReader ) {
reader = new FileReader();
reader.onloadend = function (e) {
// showUploadedItem(e.target.result, file.fileName);
jQuery("#filename").val(file.name);
};
reader.readAsDataURL(file);
}
if (formdata) {
formdata.append("images[]", file);
}
}
}
if (formdata) {
jQuery.ajax({
url: "upload.php",
type: "POST",
data: formdata,
processData: false,
contentType: false,
success: function (res) {
// document.getElementById("response").innerHTML = res;
}
});
}
}, false);
写下来,
jQuery("input[type='file']").click(function(e){
e.stopPropagation();
});
由于子进程引起的事件传播而引发错误。
如果我正确理解你,你需要触发点击事件只有当你点击h2
标签。
在这种情况下,直接添加一个标识符到您的h2
标签。
<h2 id="uploadTagId" class="inline-block">+ Upload Your Photo!</h2>
并在script.js中将click事件附加到新的标识符上。
$("#uploadTagId").click(function(e){
jQuery("input[type='file']").click();
});
相关文章:
- 将菜单项与滚动绑定时出现Jquery错误
- 试图获取表单时出现Jquery错误
- 任何人都知道IE7设置或更新/补丁,它可以防止IE因为jquery错误而无法加载页面
- 组中需要Jquery错误位置
- IE9中的Jquery错误
- 使用下拉菜单排除jquery错误
- 在文本区域检查电子邮件数据时出现奇怪的jquery错误
- 扩展对象导致jQuery错误
- 一个经典的jQuery错误 - 未捕获的类型错误:未定义不是一个函数
- 使用ng-include时发生Angular.js jQuery错误
- JQuery 错误 -- 减慢在浏览器中查看错误的操作速度
- 在页面上获取网格数据之前出现 jQuery 错误
- 如何修复我的 JQuery 错误
- jQuery 错误 SCRIPT5002:IE9 64 位中预期的函数
- jQuery错误地导致悬停
- 未定义获取 jquery 错误 $
- 将 jQuery 错误处理添加到列表、网格内的 JSF 页面上的所有图像
- 未捕获的错误:信号器 jquery 错误
- JQuery 错误的脚本顺序
- 当展开点击 li 的 ul 事件时,JS JQUERY 错误不起作用