duanchun6148
2012-05-19 19:25
浏览 327
已采纳

未被捕获的引用错误$未定义ajax-jquery

I'm trying to pass some variables using jquery and ajax but for some reason its not working. Here is the code.

<div id=' . $result['game_id'] . ' name="' . $user_id . '"  class="rate_widget" >
            <div class="star_1 ratings_stars"></div>
            <div class="star_2 ratings_stars"></div>
            <div class="star_3 ratings_stars"></div>
            <div class="star_4 ratings_stars"></div>
            <div class="star_5 ratings_stars"></div>
            <div class="total_votes">vote data</div>
        </div>

when i check chrome's developer tools i can see that the id and name attributes are set correctly .

Here is the javascript(using jquery) i put an alert box to see if the value was set but i get undefined. Just started learning ajax and jquery this week any help would be appreciated.

$('.rate_widget').each(function() {
        var widget = this;
        var out_data = {
            game_id : $(widget).attr('id'),
            user_id : $(widget).attr('name')
        };
        alert(game_id);
        $.post(
            'ratings.php',
            out_data,
            function(INFO) {
                $(widget).data( 'fsr', INFO );
                set_votes(widget);
            },
            'json'
        );

In case its not clear im trying to send game_id and user_id with the corresponding values to a php script but im getting the error. Also i know there is another question with the same name but i checked it and it didn't help.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用jquery和ajax传递一些变量,但由于某些原因它无法正常工作。 这是代码。

 &lt; div id ='。  $ result ['game_id']。  'name =“'。$ user_id。'”class =“rate_widget”&gt; 
&lt; div class =“star_1 ratings_stars”&gt;&lt; / div&gt; 
&lt; div class =“star_2 ratings_stars”&gt;&lt;  ; / div&gt; 
&lt; div class =“star_3 ratings_stars”&gt;&lt; / div&gt; 
&lt; div class =“star_4 ratings_stars”&gt;&lt; / div&gt; 
&lt; div class =“star_5 ratings_stars  “&gt;&lt; / div&gt; 
&lt; div class =”total_votes“&gt;投票数据&lt; / div&gt; 
&lt; / div&gt; 
   
 
 

当我检查chrome的开发人员工具时,我可以看到id和name属性设置正确。

这是javascript(使用jquery)我放了一个警告框来查看是否设置了值 但我得到了不确定。 刚开始学习ajax和jquery本周任何帮助都会受到赞赏。

  $('。rate_widget')。each(function(){
 var widget = this; \  n var out_data = {
 game_id:$(widget).attr('id'),
 user_id:$(widget).attr('name')
}; 
 alert(game_id); 
 $  .post(
'ratings.php',
 out_data,
 function(INFO){
 $(widget).data('fsr',INFO); 
 set_votes(widget); 
},
  n'json'
); 
   
 
 

如果不清楚我试图将带有相应值的game_id和user_id发送到php脚本但是我得到了错误 另外我知道还有另外一个同名的问题,但我检查过它并没有帮助。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • dongmi4927 2012-05-19 19:28
    最佳回答

    You can't reference game_id like that. game_id is an index in the out_data object.

    To alert the correct value you'd have to use :

    alert(out_data['game_id']);
    

    I'd also recommend wrapping your object indexes with quotes - just to make it a little more readable -

    var out_data = {
      'game_id' : $(widget).attr('id'),
      'user_id' : $(widget).attr('name')
    };
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题