weixin_33682719 2016-07-06 16:28
浏览 19

var内的jquery循环

I am hoping that somebody can help me I have a jquery function which is getting values from a php form and I need to create variable for an ajax call. Is it possible to add a loop within a var

Here is my code so hopefully will explain better what I am trying to do

...
var teacher_ids = $( '#teacher_ids' ).val();
var status = 'pending';
var array = teacher_ids.split(','); 

var data = {
  name: title,
  short_description: excerpt,
  description: content,
  status: status,
  type: 'variable',
  variations : [

This is where I am having the issue, I have multiple values which I want to be able to loop though

$.each(array,function (i, item) {
    variation_type = item.split('_'); 
    {
      regular_price: half_hour,
        attributes: [{
          id:3,
          slug: 'pa_lessonduration',
          //name: 'lessonduration',
          option: '1-hour'
        },{
          id: 1,
          slug: 'pa_weekday',
          //  name: 'weekday',
          option: variation_type[0]
        },{
          id: 2,
          slug: 'pa_daytime',
          //name: 'daytime',
          option: variation_type[1],
        }]
       //"expected an assignment or function call and instead saw an expression?" 
    }
    //"expected ( saw { "
    })

$.ajax({
        method: "PUT",
        url: POST_SUBMITTER.root + 'wc/v1/products/'+ product_id +'',
        data: data,
        beforeSend: function ( xhr ) {
            xhr.setRequestHeader( 'X-WP-Nonce', POST_SUBMITTER.nonce );
        },
        success : function( response ) {
            console.log( response );
            alert( POST_SUBMITTER.success );
        },
        fail : function( response ) {
            console.log( response );
            alert( POST_SUBMITTER.failure );
        }


    });

Any suggestion on how to get this to work please. This should print the following but I get errors

var data = {
    name: title,
    short_description: excerpt,
    description: content,
    status: status,
    type: 'variable',
    variations: [{
        regular_price: '19.99',
        attributes: [{
            id: 3,
            name: 'pa_lessonduration',
            option: '1-hour'
        }, {
            name: 'pa_daytime',
            option: '0900'
        }, {
            name: 'weekday',
            option: 'monday'
        }]
    },
    {
        regular_price: '19.99',
        attributes: [{
            id: 3,
            name: 'pa_lessonduration',
            option: '1-hour'
        }, {
            name: 'pa_daytime',
            option: '1100'
        }, {
            name: 'weekday',
            option: 'wednesday'
        }]
    }]
}

etc etc...

I hope that makes sense, if not ask and I will try to make it more clear

  • 写回答

1条回答 默认 最新

  • weixin_33727510 2016-07-07 17:39
    关注

    The code below defines the array variation to be later used in data.

    var teacher_ids = $( '#teacher_ids' ).val();
    var status = 'pending';
    var array = teacher_ids.split(','); 
    
    variation=[];                   // Declare an empty array
                                    // Then define it
    for(i=0;i<array.length;i++){
        variation_type = array[i].split('_');
    
        variation[i] = {
          regular_price: half_hour,
            attributes: [{
              id:3,
              slug: 'pa_lessonduration',
              //name: 'lessonduration',
              option: '1-hour'
            },{
              id: 1,
              slug: 'pa_weekday',
              //  name: 'weekday',
              option: variation_type[0]
            },{
              id: 2,
              slug: 'pa_daytime',
              //name: 'daytime',
              option: variation_type[1]
            }]
        }
    }
    
    
    var data = {                    // Define data
        name: title,
        short_description: excerpt,
        description: content,
        status: status,
        type: 'variable',
        variations : variation      // This is the array defined in the loop above
    }
    
    $.ajax({
        method: "PUT",
        url: POST_SUBMITTER.root + 'wc/v1/products/'+ product_id +'',
        data: data,
        beforeSend: function ( xhr ) {
            xhr.setRequestHeader( 'X-WP-Nonce', POST_SUBMITTER.nonce );
        },
        success : function( response ) {
            console.log( response );
            alert( POST_SUBMITTER.success );
        },
        fail : function( response ) {
            console.log( response );
            alert( POST_SUBMITTER.failure );
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛