从窗体中删除空输入元素 [英] Removing empty Input Elements from a form
问题描述
我有一个简单的形式,继续创建codeigniter的所有形式和验证要求。我想做的是在序列化之前过滤掉任何空输入,这样我不会创建表单输入和表单验证集规则。我对于如何去做这方面的损失。我在Jquery中的警报是我想删除任何空输入(再次序列化之前)。 在这一点上,我使用的不会检测到空表单字段。没有检测代码,整个系统工作正常。这是我使用的
< h1>字段名称< / h1>
< form action =Form.phponsubmit =return false; id =formmethod =post>
< input type =textname =v1id =v1/>
< input type =textname =v2id =v2/>
< input type =textname =v3id =v3/>
< input type =textname =v4id =v4/>
< input type =textname =v5id =v5/>
< input type =textname =v6id =v6/>
< input type =submitname =sendid =sendvalue =Send/>
< / form>
< hr />
< script>
$(function(){
$('#send')。click(function(){
------------------ ---------------------
$(:input)。each(function(){
if($(this)。 val()===)
alert(Empty Fields !!); //使用警报来查看是否检测到空字段
return false;
}
-----------------------------------------
var data = $('#form')。serialize();
$ .ajax({
type:POST,
data:data,
url: php,
success:function(msg){
if(msg){
$('#display')。html(msg).show();
} else {
$('#display')。text(< p>没有返回< / p>);
}
}
});
return false;
});
});
我只是想避免打印出空的表单字段
< p>
< label for =>< / label> < br />
< input type =textname =id =/> < br />
< label class =errorid =>此字段为必填< / label> < br />
< p />感谢您的时间 解决方案这将删除所有长度为0的文本字段:
$ '#send')。click(function(){
$(':input [type =text]')。filter(function(e){
if(this.value.length = == 0){
return true;
}
})remove();
});
例如: http://jsfiddle.net/niklasvh/ZBSyX/
I have a simple form that goes on to create all the form and validation requirements for codeigniter. What I want to do is filter out any empty inputs prior to serialization so that I do not create form inputs and form validation set rules. I am at a loss as to how to go about this. Where I have the alert in the Jquery is where I want to remove any empty inputs(again prior to serialization). At this point what I am using does not detect empty form fields. Without the detection code the entire system works fine. Here is what I am using
<h1>Field Name</h1>
<form action="Form.php" onsubmit="return false;" id="form" method="post">
<input type="text" name="v1" id="v1" />
<input type="text" name="v2" id="v2" />
<input type="text" name="v3" id="v3" />
<input type="text" name="v4" id="v4" />
<input type="text" name="v5" id="v5" />
<input type="text" name="v6" id="v6" />
<input type="submit" name="send" id="send" value="Send" />
</form>
<hr />
<script>
$(function(){
$('#send').click(function(){
---------------------------------------
$(":input").each(function() {
if($(this).val() === "")
alert("Empty Fields!!"); //using alert just to see if empty fields are detected.
return false;
});
-----------------------------------------
var data = $('#form').serialize();
$.ajax({
type: "POST",
data: data,
url: "Form.php",
success: function(msg){
if(msg){
$('#display').html(msg).show();
}else{
$('#display').text("<p>nothing came back</p>");
}
}
});
return false;
});
});
I am simply trying to avoid printing out empty form fields
<p>
<label for=""></label> <br />
<input type="text" name="" id="" /> <br />
<label class="error" id=""> This field is required</label> <br />
<p/>
Thank you for your time
解决方案 This will remove all the text fields which have a value of length 0:
$('#send').click(function(){
$(':input[type="text"]').filter(function(e){
if (this.value.length===0){
return true;
}
}).remove();
});
example: http://jsfiddle.net/niklasvh/ZBSyX/
这篇关于从窗体中删除空输入元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!