我正在研究用戶輸入的模式??捎玫妮斎肴Q于用戶單擊的按鈕/情況,例如,在一種情況下,模態應提供文本輸入,而在另一種情況下,模態應顯示單選按鈕。因此,我想用 JavaScript 動態插入我的模態的輸入元素。在一個簡單的 html 頁面中測試我的代碼有效,但不在模態中。我錯過的模態有什么特別之處嗎?如何調整我的代碼以獲取輸入元素?<html><div id="workModal" class="w3-modal"> <div class="w3-modal-content"> <span class="close">×</span> <h4>Input</h4> <div id="mod_in"></div> </div></div></html><script>var modal = document.getElementById("workModal");var span = document.getElementsByClassName("close")[0];span.onclick = function() {modal.style.display = "none";};window.onclick = function(event) {if (event.target == modal {modal.style.display = "none";}}// here starts the filling of the modal:function build_modal(info) { let element = document.getElementById("mod_in"); let inElement = ""; info.dataInputs.forEach(function (item){ inElement = document.createElement("input"); if (item.dataType.includes('string')){ inElement.setAttribute("type", "text"); } if (item.minOccurs > 0) {inElement.required = true} element.appendChild(inElement) }); element.innerHTML = inElement; let modal = document.getElementById("workModal"); modal.style.display = "block";}</script>我在我的 html 代碼中得到一個 [object HTMLInputElement] 而不是 input 元素。
如何將輸入元素動態插入到模態中?
慕容森
2021-06-16 15:49:15