简体   繁体   English

触发输入文件在第一页上的模式上单击不会触发

[英]trigger input file click on modal on first page load doesn't fire

Using angularJS, I have a modal that should trigger a input file in order to upload a file. 使用angularJS,我有一个模式,该模式应触发输入文件以上传文件。

this is the function that triggers the click 这是触发点击的功能

function triggerUploadMethod()
{
    inputFile = document.createElement('input');
    inputFile.type = 'file';
    inputFile.onchange = photoChosen;
    inputFile.click();
}

The thing that is boring me is that on the FIRST page load, when I open the modal, the trigger is not fired. 让我感到无聊的是,在第一个页面加载中,当我打开模式时,不会触发触发器。 If I close the modal and open again, the trigger WORKS, and it will keep working until the next page load... What can be happening to not work on the first page load? 如果我关闭模式并再次打开,则触发WORKS,它将一直工作到下一个页面加载...在第一个页面加载上不起作用怎么办?

This only happens on Chrome. 这仅在Chrome上发生。 On Firefox, Edge and Internet Explorer the trigger works every time, even after a page load... 在Firefox,Edge和Internet Explorer上,即使页面加载后,触发器也每次都起作用。

As a disclaimer I've never used Angular and since you didn't provide any of the other code, I went with vanilla JavaScript. 作为免责声明,我从未使用过Angular,并且由于您未提供其他任何代码,因此我使用了香草JavaScript。

As far as I can tell, the error in your code is not from what you posted above. 据我所知,代码中的错误与您上面发布的内容无关。 In the snippet below, I have taken code from W3Schools How to Make a Modal Box With CSS and JavaScript and then added your function to the part where it opens the modal on button click and it works fine (I'm on Chrome). 在下面的代码片段中,我从W3Schools的“如何使用CSS和JavaScript制作模态框”中获取了代码,然后将您的函数添加到了单击按钮即可打开模态的部分,并且可以正常工作(我在Chrome上)。

 // Get the modal var modal = document.getElementById('myModal'); // Get the button that opens the modal var btn = document.getElementById("myBtn"); // Get the <span> element that closes the modal var span = document.getElementsByClassName("close")[0]; // This variable isn't defined in your code so I just assume // it's somewhere at the top. var inputFile; // When the user clicks on the button, open the modal. // Also creates your input and clicks it. btn.onclick = function() { modal.style.display = "block"; triggerUploadMethod(); } // When the user clicks on <span> (x), close the modal span.onclick = function() { modal.style.display = "none"; } // When the user clicks anywhere outside of the modal, close it window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; } } // Create the input element and click it. function triggerUploadMethod() { inputFile = document.createElement('input'); inputFile.type = 'file'; // I have no idea what this is so I can't include it. //inputFile.onchange = photoChosen; inputFile.click(); } 
 /* The Modal (background) */ .modal { display: none; /* Hidden by default */ position: fixed; /* Stay in place */ z-index: 1; /* Sit on top */ left: 0; top: 0; width: 100%; /* Full width */ height: 100%; /* Full height */ overflow: auto; /* Enable scroll if needed */ background-color: rgb(0,0,0); /* Fallback color */ background-color: rgba(0,0,0,0.4); /* Black w/ opacity */ } /* Modal Content/Box */ .modal-content { background-color: #fefefe; margin: 15% auto; /* 15% from the top and centered */ padding: 20px; border: 1px solid #888; width: 80%; /* Could be more or less, depending on screen size */ } /* The Close Button */ .close { color: #aaa; float: right; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: black; text-decoration: none; cursor: pointer; } 
 <!-- Trigger/Open The Modal --> <button id="myBtn">Open Modal</button> <!-- The Modal --> <div id="myModal" class="modal"> <!-- Modal content --> <div class="modal-content"> <span class="close">&times;</span> <p>Some text in the Modal..</p> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM