[英]Submit only non empty fields from Form
That is the form: 这是形式:
<form action="" method="GET">
<input type="text" name="para1">
<input type="text" name="para2">
<input type="submit" value="search">
</form>
Now when I fill out only the first field I get example.com/?para1=value¶2= But I just want it to be example.com/?para1=value because para2 don't contains value. 现在,当我只填写第一个字段时,我得到example.com/?para1=value¶2=但我只想将它作为example.com/?para1=value,因为para2不包含值。 How can I do this?
我怎样才能做到这一点? Should be possible with JS or anything?
应该可以使用JS或其他什么?
Try this, 试试这个,
Include jQuery and use following snippet. 包含jQuery并使用以下代码段。
$(document).ready(function(){
$("form").submit(function(){
$("input").each(function(index, obj){
if($(obj).val() == "") {
$(obj).remove();
}
});
});
});
Something like this would be the plain javascript version: 像这样的东西将是普通的javascript版本:
<form id="theform" action="" method="GET" onsubmit="return removeEmpties()">
<input type="text" name="para1"/>
<input type="text" name="para2"/>
<input type="submit" value="search"/>
</form>
<script>
function removeEmpties() {
var form = document.getElementById("theform");
var inputs = form.children;
var remove = [];
for(var i = 0; i < inputs.length; i++) {
if(inputs[i].value == "") {
remove.push(inputs[i]);
}
}
if(remove.length == inputs.length - 1)
return false;
for(var i = 0; i < remove.length; i++)
form.removeChild(remove[i]);
return true;
}
</script>
put onclick on submit button to call submitFunc() , rather than use form action: 单击提交按钮调用submitFunc(),而不是使用表单操作:
<input type="button" onClick="submitFunc();" value="Pass Parameters"/>
js functions: js功能:
<script language="javascript" type="text/javascript">
function submitFunc()
{
loopRemove("text",3);
document.testform.action = "file:///C:/Users/mwafi/Desktop/test.html";
document.testform.submit();
}
function loopRemove(startName,count)
{
for(var i=1;i<=count;i++)
{
if(document.getElementById(startName+i).value=="")
{
var t = document.getElementById(startName+i);
t.parentNode.removeChild(t);
}
}
}
</script>
full code with HTML form: HTML表格的完整代码:
<html>
<title>Pass non-empty parameters</title>
<head>
<script language="javascript" type="text/javascript">
function submitFunc()
{
loopRemove("text",3);
document.testform.action = "http://www.google.com/";
document.testform.submit();
}
function loopRemove(startName,count)
{
for(var i=1;i<=count;i++)
{
if(document.getElementById(startName+i).value=="")
{
var t = document.getElementById(startName+i);
t.parentNode.removeChild(t);
}
}
}
</script>
</head>
<body onload="document.testform.reset();">
<form name="testform">
<h3>Pass Non-empty parameters</h3>
Parameter 1 : <input type="text" name="text1" id="text1" /><br>
Parameter 2 : <input type="text" name="text2" id="text2" /><br>
Parameter 3 : <input type="text" name="text3" id="text3" /><br><br>
<input type="button" onClick="submitFunc();" value="Pass Parameters"/>
<form>
</body>
</html>
remember don't use form action. 记住不要使用表单动作。
source: http://www.scriptsplash.com/2009/07/passing-only-non-empty-fields-on-form.html 来源: http : //www.scriptsplash.com/2009/07/passing-only-non-empty-fields-on-form.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.