使用Javascript通过变量获取数据

时间:2011-09-28 19:44:33

标签: javascript database variables cordova

我如何获取通过一段javascript代码输出的文本并将其分配到以下脚本中链接的末尾:

$(document).ready(function(){
$(document).bind('deviceready', function(){
    var name = $('#name');
    var logo = $('#logo');
    var attacking = $('#attacking');
    var midfield = $('#midfield');
    var defence = $('#defence');
    var rating = $('#rating');
    var url = 'http://79.170.44.147/oftheday.co/fifa/team.php?name=(Piece of text needs to be inputted at the end here, so that the correct data can be fetched)';

    $.ajax({
        url: url,
        dataType: 'jsonp',
        jsonp: 'jsoncallback',
        timeout: 5000,
        success: function(data, status){
           $.each(data, function(i,item){ 
                  var name1 = ''+item.name+'';
                  name.append(name1);
                  });
           $.each(data, function(i,item){ 
                  var logo2 = '<img src="'+item.logo+'" />';
                  logo.append(logo2);
                  });
           $.each(data, function(i,item){ 
                  var attacking2 = ''+item.attacking+'';
                  attacking.append(attacking2);
                  });
           $.each(data, function(i,item){ 
                  var midfield2 = ''+item.midfield+'';
                  midfield.append(midfield2);
                  });
           $.each(data, function(i,item){ 
                  var defence2 = ''+item.defence+'';
                  defence.append(defence2);
                  });
           $.each(data, function(i,item){ 
                  var rating2 = ''+item.rating+'';
                  rating.append(rating2);
                  });

           },
        error: function(){
           name.text('There was an error loading the name.');
           logo.text('There was an error loading the logo.');
           attacking.text('There was an error loading the attacking data.');
           midfield.text('There was an error loading the midfield data.');
           defence.text('There was an error loading the defence data.');
           rating.text('There was an error loading the rating data.');
        }
    });
});
});

我理解如何在PHP中执行此操作,就像在链接的末尾插入“'。$ name。'”,其中$ name等于输出的内容。我不太明白这里怎么做,用于输出前一个文本的脚本也在一个单独的javascript文件中。

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

var url = '.../oftheday.co/fifa/team.php?name='+encodeURIComponent(someVar);

变量部分来自哪里?下拉列表?

如果是,那么

var url = '.../oftheday.co/fifa/team.php?name='+encodeURIComponent($("#someId").val())

更新:

<div onload="onBodyLoad()" id="international"></div>

意味着

var url = '.../oftheday.co/fifa/team.php?name='+encodeURIComponent($("#international").text())

但是什么 的onload = “onBodyLoad()” 应该做的? - 我不相信div上有这样的属性

答案 1 :(得分:0)

不是使用字符串连接自己构建查询参数,而是最好使用data的{​​{1}}参数。请参阅http://api.jquery.com/jQuery.ajax/处的文档。

你的ajax电话会看起来像这样:

jQuery.ajax()