我希望有人可以幫助我我有一個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'
}]
}]
}
等等...
我希望是有道理的,如果不問,我會盡量做到更加清晰低於
感謝您的鼓勵之詞:)我想像你這樣擁有豐富專業知識的人不能評論以幫助像我這樣的低調的自己? – Martiniboy
不知道我完全理解這個問題。乍一看,在我看來,爲「變體」創建變量然後使用變量會比較容易,而不是試圖創建變體。需要更多信息才能真正理解。 – ckimbrell
感謝Zakarai整理我的代碼,對不起,我把它添加到我的手機很難得到格式正確,道歉,如果我的格式冒犯了我將整個功能一旦我在我的電腦前,以避免打亂任何人 – Martiniboy