【发布时间】:2017-01-21 15:42:47
【问题描述】:
我找到了这个教程,他们很好地解释了如何添加动态输入,但是,如果单击提交按钮,这不会过滤输入中的空值。这个想法是在将它们发送到服务器之前过滤这些空值,有什么想法吗?
http://www.codexworld.com/add-remove-input-fields-dynamically-using-jquery/
代码:
<?php
if(isset($_REQUEST['submit'])){
$field_values_array = $_REQUEST['field_name'];
print '<pre>';
print_r($field_values_array);
print '</pre>';
foreach($field_values_array as $value){
//your database query goes here
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add more fields using jQuery</title>
<script src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var maxField = 10; //Input fields increment limitation
var addButton = $('.add_button'); //Add button selector
var wrapper = $('.field_wrapper'); //Input field wrapper
var fieldHTML = '<div><input type="text" name="field_name[]" value=""/><a href="javascript:void(0);" class="remove_button" title="Remove field"><img src="remove-icon.png"/></a></div>'; //New input field html
var x = 1; //Initial field counter is 1
$(addButton).click(function(){ //Once add button is clicked
if(x < maxField){ //Check maximum number of input fields
x++; //Increment field counter
$(wrapper).append(fieldHTML); // Add field html
}
});
$(wrapper).on('click', '.remove_button', function(e){ //Once remove button is clicked
e.preventDefault();
$(this).parent('div').remove(); //Remove field html
x--; //Decrement field counter
});
});
</script>
</head>
<body>
<form name="codexworld_frm" action="" method="post">
<div class="field_wrapper">
<div>
<input type="text" name="field_name[]" value=""/>
<a href="javascript:void(0);" class="add_button" title="Add field"><img src="add-icon.png"/></a>
</div>
</div>
<input type="submit" name="submit" value="SUBMIT"/>
</form>
</body>
</html>
【问题讨论】:
-
您的意思是您将同时拥有静态和动态输入字段,并且在提交表单时,空输入字段将不会发送到服务器。对吗?
-
下面由 charlietfl 发布的答案怎么样?希望它对你有用。
标签: javascript php jquery html input