将输入元素添加到依赖于其他表单元素的表单

问题描述 投票:0回答:1
<form id="form" method="post">
    <input type="text" name="sonuc1" id="sonuc1" placeholder="1.Sonuç">
    <button type="submit">Ekle</button>
</form>

我想要实现的基本上是,如果 sonuc1 输入为“否”,则添加另一个名为“sonuc2”的输入元素在提交之前

javascript html ajax
1个回答
0
投票
如果您希望当此标签中输入的更改为“NO”时自动添加另一个标签,您可以使用 javascript 编写如下代码:

<form id="form" method="post"> <input type="text" name="sonuc1" id="sonuc1" placeholder="1.Sonuc" /> <button type="submit">Ekle</button> </form> <script> document.getElementById("sonuc1").addEventListener("change", function () { if (this.value.toUpperCase() === "NO") { var newInput = document.createElement("input"); newInput.type = "text"; newInput.name = "sonuc2"; newInput.placeholder = "2.Sonuc"; document .getElementById("form") .insertBefore(newInput, document.querySelector("button")); } }); </script>

© www.soinside.com 2019 - 2024. All rights reserved.