我是Javascript和jQuery的新手,我对以下代码和结果感到困惑。
为什么我的golfer_name在范围内,但我的team_id不在范围内?我该如何解决? 它似乎是数组,并且变量设置等于回调中不可用的数组元素。
jQuery代码
function submit_pick( sender ) {
var names = sender.attr('name').split('-');
var team_id = names[0];
var tournament_id = names[1];
var pick_number = names[2];
var golfer_id = sender.val();
var golfer_name = sender.find("option:selected").text();
var senders_parent = sender.parent().get(0);
sender.remove();
$(senders_parent).html('submitting pick...');
jQuery("#testdiv").append("submit pick before ajax request: "+names+', '+team_id+', '+tournament_id+', '+pick_number+', '+golfer_id+', '+golfer_name+'<br />');
jQuery.post(fantasy_golf.ajaxurl,
{'action' : 'insert_pick',
'team_id' : team_id,
'tournament_id' : tournament_id,
'pick_number' : pick_number,
'golfer_id' : golfer_id
},
function(response) {
if (response) {
jQuery("#testdiv").append("submit pick after response: "+names+', '+team_id+', '+tournament_id+', '+pick_number+', '+golfer_id+', '+golfer_name+'<br />');
}
},
'text'
);
}
#testdiv
的结果submit pick before ajax request: 10,2,1, 10, 2, 1, 22, Aaron Baddeley
submit pick after response: undefined, undefined, undefined, undefined, 22, Aaron Baddeley
答案 0 :(得分:0)
我发现有趣的是,注释掉的代码行(最初没有被注释掉)引起了上述错误,即使它们是在我认为代码问题行之后也是如此。
我很抱歉没有在我的问题之前注释掉所有额外的代码行,我正在移动一些代码并且新的我仍然需要查看下面的代码,我只是从未想过它会导致这一点上面的问题。
感谢所有帮助过的人。
function submit_pick( sender ) {
var names = sender.attr('name').split('-');
jQuery("#testdiv").append(sender.attr('name')+"<br />");
var team_id = names[0];
var tournament_id = names[1];
var pick_number = names[2];
var golfer_id = sender.val();
var golfer_name = sender.find("option:selected").text();
var senders_parent = sender.parent().get(0);
sender.remove();
$(senders_parent).html('submitting pick...');
jQuery("#testdiv").append("submit pick before ajax request: "+names+', '+team_id+', '+tournament_id+', '+pick_number+', '+golfer_id+', '+golfer_name+'<br />');
jQuery.post(fantasy_golf.ajaxurl,
{'action' : 'insert_pick',
'team_id' : team_id,
'tournament_id' : tournament_id,
'pick_number' : pick_number,
'golfer_id' : golfer_id
},
function(response) {
if (response) {
jQuery("#testdiv").append("submit pick after response: "+names+', '+team_id+', '+tournament_id+', '+pick_number+', '+golfer_id+', '+golfer_name+'<br />');
// var names = $this.attr('id').split('-');
// var pick_id = names[1];
// var team_id = names[2];
// var tournament_id = names[3];
// var pick_number = names[4];
}
},
'text'
);
}