javascript JQUERY:如何序列化具有相同名称的输入字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24127330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:13:38  来源:igfitidea点击:

JQUERY: how to serialize input fields with same name

javascriptphpjqueryajax

提问by origin

I have the following form that has lots of similar type of input fields with same name (eg. 10 fields for 'name', 10 fields for 'address'). How many times these input fields will repeated, can not be said in prior and therefore they cannot be given static different names (eg. 'name1', 'name2', 'address1', 'address2').

我有以下表单,其中包含许多具有相同名称的类似输入字段(例如,“名称”有 10 个字段,“地址”有 10 个字段)。这些输入字段将重复多少次,不能事先说出来,因此不能给它们静态不同的名称(例如,'name1'、'name2'、'address1'、'address2')。

Problem: while I am posting data using ajax post (serialized), its only posting the first value of fields with same name (received with php).

问题:当我使用 ajax post(序列化)发布数据时,它只发布具有相同名称的字段的第一个值(使用 php 接收)。

Required:

必需的:

  1. How can I get all the input data posted properly?
  2. Whats the best way to namesuch input fields that contain similar data for the purpose of catching those data with php (form is generated in php)?
  1. 如何正确发布所有输入数据?
  2. 为了用 php 捕获这些数据(表单是在 php 中生成的),为包含类似数据的此类输入字段命名的最佳方法是什么?

Sample code:

示例代码:

    <form name="content">
     <table>
      <tr>
       <td>
        <input name="full_name" type="text" />
       </td>
       <td>
        <input name="address" type="text" />
       </td>
      </tr>
      <tr>
       <td>
        <input name="full_name" type="text" />
       </td>
       <td>
        <input name="address" type="text" />
       </td>
      </tr>
    </table>
   </form>

采纳答案by Jai

I think in your case you can use $.serializeArray():

我认为在你的情况下你可以使用$.serializeArray()

var data = $('form[name="content"]').serializeArray();

this will produce something like this:

这将产生如下内容:

data = [
     {
       name : "full_name",
       value : "thefieldvalue"
     },
     {
       name : "address",
       value : "theaddressvalue"
     },
     .....
];


See this:

看到这个:

data:$('form[name="content"]').serializeArray()+'&request=insert_invoice' 

not a correct way to send data instead you can try with this below:

不是发送数据的正确方法,您可以尝试使用以下方法:

data:{
    frmvalues : $('form[name="content"]').serializeArray(), 
    request:insert_invoice
} 

回答by barell

<input name="full_name[]" type="text" value="foo" />
<input name="full_name[]" type="text" value="bar" />

In PHP it will be:

在 PHP 中,它将是:

Array (
    full_name => Array (
         0 => foo
         1 => bar
    )
)

回答by Hielke

You have to serialize the data and send it through ajax. On the php side unserialize the data and format it through this function to get the output described the comment above mine. Without it, it will won't return the desired output.

您必须序列化数据并通过ajax发送。在 php 端反序列化数据并通过此函数对其进行格式化以获得我上面的注释所描述的输出。没有它,它将不会返回所需的输出。

 public function serializedFormDatajQuery2Array($serializedArr){
                  $aFormData = array();
                  foreach($serializedArr as $aRow){

                     if(isset($aFormData[$aRow['name']]) && !is_array($aFormData[$aRow['name']])){
                        $sValue = $aFormData[$aRow['name']];
                        $aFormData[$aRow['name']] = array();
                        $aFormData[$aRow['name']][] = $sValue;
                        $aFormData[$aRow['name']][] = $aRow['value'];
                        continue;
                     }

                                if(is_array($aFormData[$aRow['name']])){
                                            $aFormData[$aRow['name']][] = $sValue;
                                            continue;
                                }

                  $aFormData[$aRow['name']] = $aRow['value'];
                  }
                             return $aFormData;
            }