Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery SheepIt演示索引使用嵌套表单是错误的_Javascript_Jquery_Sheepit - Fatal编程技术网

Javascript jQuery SheepIt演示索引使用嵌套表单是错误的

Javascript jQuery SheepIt演示索引使用嵌套表单是错误的,javascript,jquery,sheepit,Javascript,Jquery,Sheepit,我在这里使用演示: 但是,如果您有两个外部形式(地址)和两个内部形式(数字)并检查元素,您会注意到输入的名称在索引名称中仍然有#index#和/或#index#u phone#字符串 如果您尝试提交表单,那么字段数据将丢失(因为只保留该名称的最新副本)。我已经试着调试JavaScript以便修补它,但我看不出哪里出了问题。似乎normalizeForms函数没有正确处理嵌套 我可以做些什么来更正代码,使索引按预期执行?(也就是说:输入两个地址(A和B),每个地址都有两个电话号码(A1、A2和B1

我在这里使用演示:

但是,如果您有两个外部形式(地址)和两个内部形式(数字)并检查元素,您会注意到输入的名称在索引名称中仍然有
#index#
和/或
#index#u phone#
字符串

如果您尝试提交表单,那么字段数据将丢失(因为只保留该名称的最新副本)。我已经试着调试JavaScript以便修补它,但我看不出哪里出了问题。似乎
normalizeForms
函数没有正确处理嵌套

我可以做些什么来更正代码,使索引按预期执行?(也就是说:输入两个地址(A和B),每个地址都有两个电话号码(A1、A2和B1、B2),我会得到一个发布值,如:

"people" : [
   0 : {
       "addresses" : "A",
       "phones" [ 0 : "A1", 1: "A2" ]
   },
   1 : {
       "addresses" : "B",
       "phones" [ 0 : "B1", 1: "B2" ]
   }
]

(注意:我不是在寻找那种精确的格式;我可以解析任何输出,我只需要使用有意义的索引将所有数据从客户端获取到服务器,并且没有冲突。)

当涉及到嵌套输入时,这个插件的索引“规范化”端似乎存在一些基本的逻辑问题

本质上有一个
namemplate
和一个
idtemplate
,这两个元素的名称只有
%index%
%index\u phones%
在索引应该在的位置,然后是
name
id
,这两个模板应该只有
%index%
%index\u phones%
替换为实际的元素输入ID

“规范化”过程中发生的情况是,函数在这些模板上运行(每个表单每个元素一次),并根据表单的不同,使用相关索引替换
%index%
%index%
,具体取决于正在处理的表单

当函数第一次替换输入时(例如),当输入嵌套时会出现问题
%index%
比如说
0
。然后用这个值更新结果的
name
id
,比如说
person\u addresses\u 0\u phones\u%index\u phones%\u phone
。当它点击嵌套表单时,它会再次执行相同的操作,只有
%index\u phones%
。结果现在是
person\u addresses\u index%index%_phones\u 0\u phone
,因为它仍然使用未修改的模板属性,而不是已经修改了一半的
名称

为了正确地修复这个问题,插件的整个部分的逻辑确实需要重新构建,但我已经准备了一个快速补丁,可以作为临时修复

在主插件文件中,将
normalizeFieldsForForm
函数更新为:

    function normalizeFieldsForForm(form, index)
    {
        form.find(formFields).each(function(){
            var that = $(this)
                ,idTemplateAttr = getOrSetTemplate(that,"id")
                ,nameTemplateAttr = getOrSetTemplate(that, "name")
                ,idAttr = that.attr("id")
                ,nameAttr = that.attr("name")
                ,formParents = that.parents('[idtemplate]')

            /* Normalize field name attributes */
            var newNameAttr = nameTemplateAttr.replace(options.indexFormat, index);

            /* Normalize field id attributes */
            var newIdAttr = idTemplateAttr.replace(options.indexFormat, index);

            $.each(formParents,function(index,element){
                $element = $(element);
                if($element.data('indexFormat') != options.indexFormat){
                    /* Normalize field name attributes */
                    newNameAttr = newNameAttr.replace($element.data('indexFormat'), $element.data('formIndex'))

                    /* Normalize field id attributes */
                    newIdAttr = newIdAttr.replace($element.data('indexFormat'), $element.data('formIndex'))
                }
            });

            form.find("label[for='"+idAttr+"']").each(function(){
                $(this).attr("for", newIdAttr);
            });

            that.attr("id", newIdAttr);
            that.attr("name", newNameAttr);
        });
    }
然后更新
addForm
函数。在未修改的插件文件中的
385
行附近添加该行

    // Index format
    newForm.data('indexFormat', options.indexFormat);
越界

    // Index
    newForm.data('formIndex', getIndex());
在插件作者着手解决逻辑问题之前,这应该是一个解决方案。这适用于插件版本
1.1.1