简体   繁体   English

如何选择“ <td> &#39;并使用jquery导航到url?

[英]How do I select a '<td>' and navigate to url using jquery?

I am using jquery to pull football data from an api as follows 我正在使用jquery从api中提取足球数据,如下所示

type: 'GET',
dataType: 'json',
url: "http://api.football-data.org/v1/competitions/426/teams",
processData: true,
//idLength: url.Length - url.LastIndexOf("/", StringComparison.Ordinal) - 1,
//id: url.Substring(url.LastIndexOf("/", StringComparison.Ordinal) + 1, idLength),
success: function (data, status) {
    var trHTML = '';
    $.each(data.teams, function (key, item) {
        var id = item._links.self.href;
        var indexOfLastBackSlash = id.lastIndexOf("/") + 1;
        id = id.substring(indexOfLastBackSlash);
        //$('<option>', { text: item.TeamName, id: item.ID }).appendTo('#lstTeams');
        trHTML += '<tr class="info"><td>' + item.name +
                  '</td><td>' + item.code +
                  '</td><td>' + item.shortName +
                  '</td><td>' + item.squadMarketValue +
                  '</td><td>' + item.crestURL +
                  '</td><td>' + id +
                  '</td></tr>';

I want to be able to select a 'td' and navigate to another url like " http://api.football-data.org/v1/teams/322/fixtures " for the team with id of 322. How can I do so? 我希望能够为ID为322的团队选择一个“ td”并导航到另一个URL,例如“ http://api.football-data.org/v1/teams/322/fixtures ”。我该怎么办所以?

You can create a function which will do same ajax request but this time for single record. 您可以创建一个function ,该function将执行相同的ajax request但这一次仅用于单个记录。 On your target's click event you need to call that function passing the id you need to fetch records for. 在目标的click event您需要调用该函数,并传递您需要为其获取记录的id One way of doing this is to set a data attribute to your target container. 一种方法是为目标容器设置data attribute

Below is the sample code performing similar functionality. 下面是执行类似功能的示例代码。

$(function() {
    $.ajax({
        url: 'https://jsonplaceholder.typicode.com/posts/',
        method: 'GET',
        dataType: 'json'
    }).then(function(Data) {
        $.each(Data, function (key, data) {
        var id = data.id;
        var title = data.title;
        $('.tableAllRecords').append('<tr><td><a href=# class=mylink data-id='+id+'>'+id+'</a></td><td>'+title+'</td></tr>');
      });
    });
});

function fetchSingle(id) {
    $.ajax({
        url: 'https://jsonplaceholder.typicode.com/posts/' + id,
        method: 'GET',
        dataType: 'json'
    }).then(function(data) {
        var id = data.id;
        var title = data.title;
        var body = data.body;
        $('.tableSingleRecord').append('<tr><td>'+id+'</td><td>'+title+'</td><td>'+body+'</td></tr>');
    }); 
}

$(document).on('click', '.mylink', function(e) {
    var id = $(this).data('id');
    fetchSingle(id);
    e.preventDefault(); // disabling anchor's default [redirect] behaviour
});

Here is the link to fiddle. 这是小提琴的链接。 https://jsfiddle.net/2h54vu7h/ https://jsfiddle.net/2h54vu7h/

If you need to display records on a new page, you simply have to pass id into your url and on next page you have make an ajax request getting the id variable from url . 如果需要在新页面上显示记录,只需将id传递到url然后在下一页上发出ajax请求,以从url获取id变量。

Example: '<a href=somepage.html?id='+id+'>View Details</a>'; 示例: '<a href=somepage.html?id='+id+'>View Details</a>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM