簡體   English   中英

觸發輸入文件在第一頁上的模式上單擊不會觸發

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

使用angularJS,我有一個模式,該模式應觸發輸入文件以上傳文件。

這是觸發點擊的功能

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

讓我感到無聊的是,在第一個頁面加載中,當我打開模式時,不會觸發觸發器。 如果我關閉模式並再次打開,則觸發WORKS,它將一直工作到下一個頁面加載...在第一個頁面加載上不起作用怎么辦?

這僅在Chrome上發生。 在Firefox,Edge和Internet Explorer上,即使頁面加載后,觸發器也每次都起作用。

作為免責聲明,我從未使用過Angular,並且由於您未提供其他任何代碼,因此我使用了香草JavaScript。

據我所知,代碼中的錯誤與您上面發布的內容無關。 在下面的代碼片段中,我從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