本文介绍了跳过Jquery序列化中的隐藏输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些带有隐藏和可见输入的表单,如下所示.在这个示例中,我只想序列化没有隐藏的name ='country'

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());

推荐答案

使用以下代码. not(:hidden)将不包括隐藏在serialize()

use below code . not(:hidden) will not include input type hidden in serialize()

检查演示

var frm  = $('#finalform').find(":input:not(:hidden)").serialize();

var frm  = $('#finalform :input:not(:hidden)').serialize();

演示

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>

这篇关于跳过Jquery序列化中的隐藏输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 22:09