Dynamically add form elements using jQuery/JavaScript. This is useful when the DOM is already loaded and you need to add additional input fields based on user action or selection (for example). You can create any new form element, not just input fields. Alternatively, you can create input elements in the page and simply hide them and display them again if needed.
Example: When changing password1
, insert a new input box named password1
after it is called password2
.
// 监听 password1 字段的 change 事件以提示添加新输入框 $('#password1').change(function() { // 动态创建新的输入框并将其插入到 password1 之后 $('<input type="password" id="password2" name="password2" placeholder="Confirm Password">').insertAfter('#password1'); });
The above is the detailed content of jQuery Dynamically Add Form Elements. For more information, please follow other related articles on the PHP Chinese website!