我有这个代码动态地添加字段,并且无处不在添加新字段的按钮开始表现得很奇怪。我工作得很好,没有任何东西,它开始像提交按钮一样处理表单。我尝试将按钮设置为按钮,然后停止添加字段。
html:
<form method="post" action="setup5.php">
<div class="input_fields_wrap">
<button class="add_field_button">
Add More Fields
</button>
<div>
<input type="text" name="mytext[]">
</div>
</div>
<div>
<input type="submit" value="Next" />
</div>
</form>
JS:
<script>
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div><input type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
</script>