jQuery: form serialize, hidden fields, and not displayed fields

偶尔善良 提交于 2019-12-23 07:39:06

问题


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?


回答1:


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



回答2:


You can use $form.find(':visible').serialize()




回答3:


var $disabled_list = $(this).find('input:hidden,select:hidden,textarea:hidden').not('input[type=hidden]').attr('disabled', 'disabled');

tried this way and it seems to work



来源:https://stackoverflow.com/questions/9375797/jquery-form-serialize-hidden-fields-and-not-displayed-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!