2016-07-06 41 views
-2

我希望有人可以幫助我我有一個jQuery的函數,它是從一個PHP窗體中獲取值,我需要爲ajax調用創建變量。是否有可能一個變種Jquery循環內var

這裏中添加一個循環是我的代碼,所以希望將更好地解釋什麼,我試圖做

... 
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 : [ 

這是我遇到的問題,我有多個值,其我希望能夠循環雖然

$.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); 
     } 


    }); 

任何建議如何讓這個工作請。這應該打印以下,但我得到的錯誤

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' 
     }] 
    }] 
} 

等等...

我希望是有道理的,如果不問,我會盡量做到更加清晰低於

+3

感謝您的鼓勵之詞:)我想像你這樣擁有豐富專業知識的人不能評論以幫助像我這樣的低調的自己? – Martiniboy

+0

不知道我完全理解這個問題。乍一看,在我看來,爲「變體」創建變量然後使用變量會比較容易,而不是試圖創建變體。需要更多信息才能真正理解。 – ckimbrell

+1

感謝Zakarai整理我的代碼,對不起,我把它添加到我的手機很難得到格式正確,道歉,如果我的格式冒犯了我將整個功能一旦我在我的電腦前,以避免打亂任何人 – Martiniboy

回答

0

代碼定義數組variation以後將用於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); 
    } 
}); 
+0

非常感謝你** Louys Patrice Bessette **,我只是需要在循環之前聲明一個變量_類型的空數組,並且它工作得很好,爲什麼這不適用於$ .each?在我以前的一個測試中,我試圖在將數據添加到數據var之前定義一個變體,但它沒有起作用,但是使用了$ .each – Martiniboy

+0

'$ .each()'可以循環使用一組HTML元素。這個集合必須在它之前指定。例如:'$(「。item」)。each(function(){//對每個具有類項目的元素執行一些操作}爲了遍歷一個數組,循環一個javascript'for'循環是最好的。 –

+0

有道理,謝謝 – Martiniboy