gpt4 book ai didi

javascript - 避免通过动态输入(HTML - Jquery)发送空值

转载 作者:行者123 更新时间:2023-11-30 15:34:30 25 4
gpt4 key购买 nike

我找到了这个教程,他们在其中很好地解释了如何添加动态输入,但是,如果单击提交按钮,这不会从输入中过滤空值。这个想法是在将它们发送到服务器之前过滤那些空值,一些想法?

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>

最佳答案

一种方法是删除提交事件处理程序中的空输入

$('form[name=codexworld_frm']).submit(function(){
$(this).find('input').filter(function(){
return !this.value;
}).remove();
})

关于javascript - 避免通过动态输入(HTML - Jquery)发送空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41781231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com