我正在为用户输入创建模态。可用的输入取决于用户单击的按钮/情况,例如,在一种情况下,模式应提供文本输入,而在另一种情况下,模式应显示单选按钮。因此,我想使用JavaScript动态插入模态的输入元素。
在一个简单的html页面中进行了测试,我的代码可以运行,但不能在模式内运行。我错过的模态有什么特别之处吗?如何调整代码以获取输入元素?
<html lang="en">
<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元素。
您必须使用下面的代码在html中添加新元素:
$("button").click(function(){ //you can call your function after any events
$("p").append("<b>Appended text</b>"); //you can append every element instead of <b>
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句