How would I go about fetching a piece of text that has been outputted via a piece of javascript code and assigning it to the end of the link in the below script:
$(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.');
}
});
});
});
I understand how to do this in PHP it would be something like inserting "'.$name.'" to the end of the link where $name is equal to the outputted content. I don't quite understand how to this here though, the script used to output the previo开发者_开发百科us text is in a separate javascript file too.
Any help would be greatly appreciated!
var url = '.../oftheday.co/fifa/team.php?name='+encodeURIComponent(someVar);
where does the variable part come from? A dropdown?
If so then
var url = '.../oftheday.co/fifa/team.php?name='+encodeURIComponent($("#someId").val())
UPDATE:
<div onload="onBodyLoad()" id="international"></div>
would mean
var url = '.../oftheday.co/fifa/team.php?name='+encodeURIComponent($("#international").text())
But what is onload="onBodyLoad()" supposed to do? - I do not believe there is such an attribute on a div
Rather than constructing the query parameters yourself with string concatenations you would be better off using the data
parameter of jQuery.ajax()
. See the docs at http://api.jquery.com/jQuery.ajax/.
Your ajax call would then look something like this:
// Notice that there is no name= in the url
var url = 'http://79.170.44.147/oftheday.co/fifa/team.php';
$.ajax({
url: url,
data: {
name: $("#international").text() // this will put the name=... in the query
},
dataType: 'jsonp',
精彩评论