gpt4 book ai didi

javascript - 使用 Javascript 按变量获取数据

转载 作者:行者123 更新时间:2023-11-28 10:09:54 25 4
gpt4 key购买 nike

我将如何获取通过一段 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 文件中。

任何帮助将不胜感激!

最佳答案

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 上有这样的属性

关于javascript - 使用 Javascript 按变量获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7588712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com