简体   繁体   English

如何检查输入类型=“文件”是否选择了文件?

[英]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.

相关问题 如何检查文件是否已被选中 <input type=“file”> 元件? - How do I check if a file has been selected in a <input type=“file”> element? 有没有办法找出通过输入字段(type = file)选择了多少文件? - is there any way to findout how many file has been selected through input field(type=file)? 隐藏输入类型=“文件”时如何选择该文件? - How to signal that file has been selected when the input type=“file” is hidden? 如果用户在Android 2.3.7的文件输入中选择了文件,如何检入JS - How to check in JS if user selected a file in file input on Android 2.3.7 如何显示在输入类型文件中选择了哪个文件? - How to show which file is selected in Input type File? 如何使用获取所选文件的完整路径<Input Type=“file” /> - How to get a full path of a selected file using <Input Type=“file” /> Java脚本 <input type=“file”> 即使选择了文件也具有空文件列表 - Javascript <input type=“file”> has empty file list even when file is selected 样式输入 type=&quot;file&quot; - 没有出现“未选择文件” - Styling input type="file" - no "No file selected" appear 更改输入类型文件选择的名称文件 - Change name File selected by input type file 文件被选中后是否可以更改<input type="“file”">并且仍然阅读其文本? - Can a file be changed after it has already been selected in <input type=“file”> and still have its text read?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM