我知道如何使用Javascript处理输入字段。但是当我使用javascript createElement方法动态创建一个输入字段时,我不能使用它。由于我还没有开始学习PHP,所以我想知道如何使用javascript处理动态创建的输入域。
发布于 2018-08-06 02:35:30
发布于 2018-08-06 03:03:33
您可能忘记了将输入字段添加到文档正文中。使用document.createElement
创建元素时,千万不要忘记在创建完成后将其附加到主体中。
var input = document.createElement("input");
document.body.appendChild(input);
<span id="result"></span><br/>
<script>
var input = document.createElement("input");
input["type"] = "text";
document.body.appendChild(input);
input.addEventListener("keypress", function(e){
document.getElementById('result').textContent = "Key: "+e.key+" KeyCode: "+e.keyCode+" Value: "+this.value+e.key;
});
</script>
https://stackoverflow.com/questions/51697145
复制相似问题