admin管理员组文章数量:1193986
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/?para1=value¶2= But I just want it to be example/?para1=value because para2 don't contains value. How can I do this? Should be possible with JS or anything?
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. How can I do this? Should be possible with JS or anything?
Share Improve this question edited Jul 12, 2023 at 12:48 gavenkoa 48.7k28 gold badges268 silver badges333 bronze badges asked May 8, 2014 at 4:27 Eddy UnruhEddy Unruh 3091 gold badge4 silver badges9 bronze badges 6 | Show 1 more comment3 Answers
Reset to default 20Try this,
Include jQuery and use following snippet.
$(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:
<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:
<input type="button" onClick="submitFunc();" value="Pass Parameters"/>
js functions:
<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>
<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
本文标签: javascriptSubmit only non empty fields from FormStack Overflow
版权声明:本文标题:javascript - Submit only non empty fields from Form - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738451914a2087533.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'$('##').prop(disabled, true);
but better to handle this in your controller/action method – user3559349 Commented May 8, 2014 at 4:32