jQuery:形式序列化,隐藏字段,而不是显示字段 [英] jQuery: form serialize, hidden fields, and not displayed fields

查看:600
本文介绍了jQuery:形式序列化,隐藏字段,而不是显示字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在提交表单时使用 $(this).serialize()

,除了当我(出于某种原因)有两个相同名称的字段(一个可见,一个不是,我不是在谈论 type =visible但是 display:none )...



但当然 serialize

我试过这个



<$ p $ ('input':hidden,select:hidden,'textarea:hidden')。attr('disabled','disabled');
$(this).serialize();
$ disabled_list.attr('disabled','');

它解决了我的问题,除了:hidden selector,还需要type =hidden



正确的方法是什么? 您不必打电话< code>< form>< / code>本身,你可以匹配它的一些控件并在结果上调用它组。这允许您避免修改 disabled 属性。



因为您想要控件匹配:隐藏只有当它们实际公开隐藏类型时,您可以使用以下选择器:

  $(this).find(input [type ='hidden'],:input:not(:hidden))。serialize(); 


I am using $(this).serialize() when submitting a form.

It works well, except in times when I (for some reason) have 2 fields with same name (one visible, and one not, and I am not talking about type="visible" but display:none)...

But of course serialize has no regard for this... it just takes them all.

I tried this

var $disabled_list = $(this).find('input:hidden,select:hidden,textarea:hidden').attr('disabled', 'disabled');
$(this).serialize();
$disabled_list.attr('disabled','');

and It is solving my problem, except the :hidden selector, takes also type="hidden"

what's the proper way?

解决方案

You do not have to call serialize() on the <form> itself, you can match some of its controls and call it on the resulting set. This allows you to avoid tinkering with disabled attributes.

Since you want controls matching :hidden only if they also actually expose the hidden type, you can use the following selector:

$(this).find("input[type='hidden'], :input:not(:hidden)").serialize();

这篇关于jQuery:形式序列化,隐藏字段,而不是显示字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆