提交禁用输入作为jQuery Ajax请求的一部分的最佳方法是什么? [英] What is the best way to submit disabled inputs as part of a jQuery Ajax request?

查看:103
本文介绍了提交禁用输入作为jQuery Ajax请求的一部分的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常复杂的表格,可以在用户输入数据时使用它向用户提供实时验证和反馈.表单的某些部分可能由于输入的表单业务逻辑"而被禁用,但是服务器端需要知道这些禁用的输入的值,以便在Ajax请求中进行某些检查.

I have a pretty complex form that provides real-time validation and feedback to the user using it as they type in data. Parts of the form may be disabled due to form "business logic" of the inputs, but the server side needs to know the value of these disabled inputs to do some of its checking in the Ajax requests.

我使用jQuery序列化整个表单$(form).serialize(),并在我的Ajax请求期间将所有这些数据发送到服务器,但是此序列化的数据将丢失所有禁用的输入.那么,在每个Ajax请求中包括所有表单数据(甚至禁用的输入)的最佳方法是什么?

I was using jQuery to serialize the entire form, $(form).serialize(), and sending all of this data to the server during my Ajax requests, but this serialized data will be missing any inputs that are disabled. So what is the best way to include all of the form data (even disabled inputs) with each Ajax request?

我当前的想法是将一个函数附加到ajaxStart(回调),该函数将创建所有表单输入的disabled属性的客户端哈希映射,启用所有输入,进行序列化,然后将每个表单的disabled属性设置为其原始值.但这似乎太复杂了,我想要一个更简单的东西,它不会那么脆弱.

My current thought is to attach a function to ajaxStart (callback) that would create a client side hash-map of all form inputs' disabled attribute, enable all the inputs, serialize, and then set each form's disabled attribute back to its original value. But this seems overly complex, and I wanted something simpler that wouldn't be as brittle.

推荐答案

尝试执行以下操作来序列化表格:

Try doing something like this to serialize the form:

   $(function() {
      $('input[type=button]').click( function() {
          var form = $('form').serialize();
          $('input[disabled]').each( function() {
              form = form + '&' + $(this).attr('name') + '=' + $(this).val();
          });

      });
   });

测试HTML

<form action="/action">
   <input type='text' disabled='disabled' value='hey' id='hey' name='hey' />
   <input type='text' value='ho' id='ho' name='ho' />
   <input type='text' value="hi" id='hi' name='hi' />
   <input type='button' value="Serialize" />
</form>

输出

ho=ho&hi=hi&hey=hey

这篇关于提交禁用输入作为jQuery Ajax请求的一部分的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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