2012-04-25 233 views
-1

我想在Json回调中定义一个变量。我如何在Json中定义一个变量?

代码;

$("select").change(function() { 
      var $variable = ""; 
      $("select option:selected").each(function() { 
       $variable += $(this).text() + " "; 
       }); 
      $("div.yaz").text($variable); 

     $('#result').html('loading...'); 

    $.getJSON('program-bilgileri.php', function(JSON){ 
     $('#result').empty(); 

     $.each(JSON.$variable, function(i, program){ 
      $('#result') 
      .append(program.isim +'<br />') 
      .append(program.bilgi+'<br />') 
      .append(program.adres+'<hr />'); 
     }); 
    }); 
    }) 
    .trigger('change'); 

program-bilgileri.php returns;

{ 
    "programlar":[ 
     { 
     "isim":"Zone Alarm", 
     "bilgi":"bilgisayarın güvenliğini sağlar", 
     "adres":"www.zonealarm.com" 
     }, 
     { 
     "isim":"Opera", 
     "bilgi":"güvenli ve hızlı bir web tarayıcısıdır", 
     "adres":"www.opera.com" 
     }, 
     { 
     "isim":"Photoshop", 
     "bilgi":"güçlü bir imaj işleme yazılımıdır", 
     "adres":"www.adobe.com" 
     } 
    ] 
} 

的问题是在这里 “$。每个(JSON。$变量,函数(I,程序)” 如果我JSON定义$变量它不工作。

任何想法?

+0

我不认为这段代码的确如你所想。期望'JSON。$ variable'做什么?如果您期望它访问具有与$ variable的值相等的对象的键,则会假设错误的事物。那将是JSON [$ variable]。 – Corbin 2012-04-25 10:38:45

+0

从'getJSON'请求中发布返回的JSON。 – 2012-04-25 10:39:36

+0

@Corbin我尝试JSON [$变量],但它不起作用。 – mrchad 2012-04-25 13:52:03

回答

1

我看到的问题是

  • 里面你正在使用$("select option:selected")指找到页面中所有select元素change事件,而不仅仅是一个改变。
    改为使用$(this).children('option:selected')
  • 我假设你是让select元素的多重选择,这就是为什么你与$variable+= ..(你也是在最后加空格)。这意味着,虽然,该变量将是类似"programlar ""programlar somethingelse"。 您返回的JSON虽然有一个密钥programlar。一个单词,没有空格..所以当你做JSON[$variable]这是正确的方式来访问基于变量名称的元素,它不匹配。

如果<select>元素不允许多个选择,那么解决的办法是

$("select").change(function() { 
    var $variable = $(this).children("option:selected").text(); 

    $("div.yaz").text($variable); 

    $('#result').html('loading...'); 

    $.getJSON('program-bilgileri.php', function(JSON) { 
     $('#result').empty(); 
     $.each(JSON[$variable], function(i, program) { 
      $('#result') 
       .append(program.isim + '<br />') 
       .append(program.bilgi + '<br />') 
       .append(program.adres + '<hr />'); 
     }); 
    }); 
}).trigger('change'); 

如果它确实是一个多项选择和每个选项可以在JSON出现,那么你必须检查每个选项在变量中找到。

$("select").change(function() { 
    var $variable = $(this).children("option:selected").map(function(){ 
          return $(this).text(); 
        }).get(); 

    $("div.yaz").text($variable.join(' ')); 

    $('#result').html('loading...'); 

    $.getJSON('program-bilgileri.php', function(JSON) { 
     $('#result').empty(); 

     for (index=0, length = $variable.length; index < length; index ++) { 
      $.each(JSON[$variable[index]], function(i, program) { 
       $('#result') 
        .append(program.isim + '<br />') 
        .append(program.bilgi + '<br />') 
        .append(program.adres + '<hr />'); 
      }); 
     } 
    }); 
}).trigger('change'); 
0

尝试

$.each(JSON['programlar'], function(i, program) ...); 

这将遍历您返回JSON对象,这部分从PHP:

{ 
    "isim":"Zone Alarm", 
    "bilgi":"bilgisayarın güvenliğini sağlar", 
    "adres":"www.zonealarm.com" 
    }, 
    { 
    "isim":"Opera", 
    "bilgi":"güvenli ve hızlı bir web tarayıcısıdır", 
    "adres":"www.opera.com" 
    }, 
    { 
    "isim":"Photoshop", 
    "bilgi":"güçlü bir imaj işleme yazılımıdır", 
    "adres":"www.adobe.com" 
    } 
相关问题