溫馨提示×

如何用input屬性創(chuàng)建動態(tài)表單

小樊
90
2024-07-03 12:25:11
欄目: 編程語言

可以通過JavaScript來動態(tài)地創(chuàng)建表單元素并為其添加屬性。以下是一個示例:

<!DOCTYPE html>
<html>
<head>
    <title>動態(tài)表單</title>
</head>
<body>

<form id="myForm">
    <label for="name">姓名:</label>
    <input type="text" id="name" name="name"><br><br>
</form>

<button onclick="addInput()">添加輸入框</button>

<script>
function addInput() {
    var form = document.getElementById("myForm");
    var input = document.createElement("input");
    input.type = "text";
    input.name = "newInput";
    form.appendChild(input);
}
</script>

</body>
</html>

在上面的示例中,當點擊按鈕時,會動態(tài)地在表單中添加一個輸入框。你也可以根據(jù)自己的需求添加更多的表單元素,并為其設置不同的屬性。

0