【问题标题】:Send multiple ajax data from dynamically generated fields从动态生成的字段发送多个 ajax 数据
【发布时间】:2015-06-03 07:40:40
【问题描述】:

如何从 ajax 中动态生成的字段发送多个数据?

因为我无法提前知道字段的数量,所以我使用了一个对我不起作用的 for 循环。

$.ajax({
            type: 'GET',
            data:{
                expectationPercentagePairId: JSON.stringify(expectationPercentagePairIds),
                expectationScores: expectationScores,
                for(var id in expectationPercentagePairIds){
                    score-${id}: $("input[name='score-'+id]")
                }
            },
            url:'${g.createLink( controller:'review', action:'saveReview', params:[id: params.id] )}'
        });

【问题讨论】:

  • 你可以将数据作为数组/json传递
  • 字段名称不同。我可以传递带有数据来源字段名称的数组吗?
  • 这个expectPercentagePairIds的内容是什么?
  • 它是一个数组,字段名称是score-id。这里 id 是一个变化的值。
  • 你仍然可以发送它,expectationPercentagePairIds :expectationPercentagePairIds

标签: jquery ajax loops grails


【解决方案1】:

循环在这里不起作用。您可以使用序列化传递所有表单数据,然后在后端处理数据。

$.ajax({
        type: 'GET',
        data:{
            $('form').serialize()
        },
        url:'${g.createLink( controller:'review', action:'saveReview', params:[id: params.id] )}'
    });

【讨论】:

  • 就像我说的,但他不想那样
  • @YidnekachewWondimu 很高兴听到这个消息!
【解决方案2】:

如果您动态生成的字段位于静态父元素中,您可以找到所有输入,例如 $("#perantelem").find('input'),然后您可以使用您的 id(score-${id}) 进行过滤并生成 key/value(name : value)数组或json

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 2011-03-27
    • 1970-01-01
    • 2013-05-15
    • 2017-02-03
    相关资源
    最近更新 更多