2013-04-21 62 views
0

所以我使用加载函数将数据传递给另一个名为compare_proc.php的文件..我创建了一个变量,用于存储要传递的数据,它们是数字..当我提醒这些变量,数据是存在的,但是,当我通过加载功能,将数据传递,变量的内容迷路..下面是相关的代码我的jquery不能识别变量值有什么问题

<script type="text/javascript"> 

     jQuery(document).ready(function() { 
      jQuery('#mycarousel').jcarousel(); 
      $("a.inline").colorbox({iframe:true, width:"80%", height:"80%"}); 
       $("#opposition a").click(function(e) { 
       var first_id = $(this).attr('id'); // value of first_id is 10 
       var second_id = $("h1").attr('id'); // value of second_id is 20 
       $("div#test").load('compare_proc.php','id=first_id&id2= second_id'); 
       e.preventDefault(); 
       }); 
    }); 

然而,装载功能通过first_id而不是10到id和second_id而不是20到id2 ..我哪里错了?

回答

3

你需要做字符串连接,因为first_idsecond_id是变量,你需要像'id=' + first_id + '&id2=' + second_id创建一个连接字符串作为参数

jQuery(document).ready(function() { 
    jQuery('#mycarousel').jcarousel(); 
    $("a.inline").colorbox({iframe:true, width:"80%", height:"80%"}); 

    $("#opposition a").click(function(e) { 
     var first_id = $(this).attr('id'); // value of first_id is 10 
     var second_id = $("h1").attr('id'); // value of second_id is 20 
     $("div#test").load('compare_proc.php','id=' + first_id + '&id2=' + second_id); 
     e.preventDefault(); 
    }); 
}); 

另一种方法是通过将数据作为一个对象,而不是字符串下面给出的,我喜欢这种方法

jQuery(document).ready(function() { 
    jQuery('#mycarousel').jcarousel(); 
    $("a.inline").colorbox({iframe:true, width:"80%", height:"80%"}); 

    $("#opposition a").click(function(e) { 
     var first_id = $(this).attr('id'); // value of first_id is 10 
     var second_id = $("h1").attr('id'); // value of second_id is 20 
     $("div#test").load('compare_proc.php',{ 
      id:first_id, 
      id2:second_id 
     }); 
     e.preventDefault(); 
    }); 
}); 
+0

谢谢,我忘了这一点! – 2013-04-21 07:18:23

1

只需更换这行:

$("div#test").load('compare_proc.php','id=first_id&id2= second_id'); 

有了这个:

$("div#test").load('compare_proc.php','id=' + first_id + '&id2=' + second_id); 
相关问题