I have forms with inputs of hidden and visible like below.In this example I want to serialize only name='country' which is not hidden
<form id="finalform">
<input type='hidden' name='sid' value='901271316' />
<input type='hidden' name='tco_currency' value='USD'>
<input name='country' value='India' />
</form>
I tried below code but its serializing even hidden inputs.
var frm = $("form#finalform:visible");
var add= JSON.stringify(frm.serializeArray());
use below code . not(:hidden)
will not include input type hidden in serialize()
check DEMO
var frm = $('#finalform').find(":input:not(:hidden)").serialize();
Or
var frm = $('#finalform :input:not(:hidden)').serialize();
DEMO
var frm = $('#finalform :input:not(:hidden)').serializeArray();
var frm1 = $('#finalform').find(":input:not(:hidden)").serializeArray();
$('#firstOutput').text(JSON.stringify(frm))
$('#secondOutput').text(JSON.stringify(frm1))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<form id="finalform">
<input type='hidden' name='sid' value='901271316' />
<input type='hidden' name='tco_currency' value='USD'>
<input name='country' value='India' />
</form>
<div id="firstOutput"> </div>
<div id="secondOutput"> </div>
I find this a cleaner way of doing it:
var frm = $('#finalform [type!="hidden"]').serialize()
You can make a temporary copy of it and remove the hidden inputs from it before serializing :
var form = $("#finalform").clone();
$(form).find("input[type=hidden]").remove()
var serialized = $(form).serializeArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With