[英]How to check an input type=“file” has a file selected?
I am developing an app with HTML, JavaScript and CSS3 with VS2012 (MetroApp) 我正在使用VS2012(MetroApp)开发一个带有HTML,JavaScript和CSS3的应用程序
I need to trigger a verification for enabling or disabling a button after adding a file on my 在我的文件上添加文件后,我需要触发启用或禁用按钮的验证
<input type="file"/>
so far I have this: 到目前为止我有这个:
HTML: default.html: HTML:default.html:
<!DOCTYPE html>
<html>
<head>
<title>myApp</title>
<link href="/css/default.css" rel="stylesheet" />
<script src="/js/default.js"></script>
</head>
<body>
<div id="content">
<h1>myApp</h1>
<br /><br />
<button id="imageCaptureButton">Take Picture</button>
<input type="file" id="uploadCaptureInputFile"/><br />
<button id="uploadCaptureButton">Subir Foto</button>
<br /><br />
<div id="result"></div>
</div>
</body>
</html>
JavaScript: default.js JavaScript:default.js
function getDomElements() {
_uploadCaptureButton = document.querySelector("#uploadCaptureButton");
_uploadCaptureInputFile = document.querySelector("#uploadCaptureInputFile");
}
function wireEventHandlers() {
_uploadCaptureInputFile.addEventListener("onchange", verifyControls, false);
}
app.onloaded = function () {
getDomElements();
wireEventHandlers();
_uploadCaptureButton.disabled = true;
}
function verifyControls() {
if ( _uploadCaptureInputFile != "") {
_uploadCaptureButton.disabled = false;
}
else {
_uploadCaptureButton.disabled = true;
}
}
But I can't make it work, it seems that 但我似乎无法使其发挥作用
_uploadCaptureInputFile.addEventListener("onchange", verifyControls, false);
it doesn't obey, is there a way to assign this event to my inputType for trigger such verification?? 它不服从,是否有办法将此事件分配给我的inputType以触发此类验证?
thanks in advance 提前致谢
When listening with addEventListener
, you listen for the change event which is just "change"
, not "onchange"
. 在使用
addEventListener
侦听时,您将侦听仅仅是"change"
而不是"onchange"
的更改事件。 Listening for "onchange"
will never fire because there is never a matching event fired (unless you create a custom one yourself). 听
"onchange"
永远不会触发,因为从未触发匹配事件(除非您自己创建自定义事件)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.