【问题标题】:What's wrong with my Jquery not recognizing variable value [closed]我的 Jquery 无法识别变量值有什么问题[关闭]
【发布时间】:2013-04-21 06:56:35
【问题描述】:

所以我使用加载函数将数据传递到另一个名为 compare_proc.php 的文件。我创建了存储要传递的数据的变量,这些数据是数字。当我提醒这些变量时,数据就在那里,但是,当我通过加载函数传递数据时,变量的内容会丢失..下面是相关代码

<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.. 我哪里出错了?

【问题讨论】:

    标签: javascript jquery jquery-load


    【解决方案1】:

    您需要进行字符串连接,因为first_idsecond_id 是变量,您需要创建像'id=' + first_id + '&amp;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();
        });
    });
    

    【讨论】:

    • 谢谢,我忘了!
    【解决方案2】:

    只需替换这一行:

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

    有了这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-17
      • 1970-01-01
      • 2013-03-26
      • 1970-01-01
      • 2021-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多