weixin_33701294 2015-02-26 18:17 采纳率: 0%
浏览 47

Ajax表中的按钮链接

Please look at the ajax code below, As you can see, i have managed to make a button display in each table row, which is great, the issue is each button will only link ti the dataurl, I'm really needing to make it so that each button will link off to a different place, But only is the event is still available. E.g. if a row disappears i need the button to disappear? So the Increment feature wouldn't work.

Thanks Sam

Heres the ajax code

$.ajax({
    type: 'GET',
    crossDomain: true,
    dataType: 'json',
    url: 'api link for seatwave',
    success: function(json) {
        //var json = $.parseJSON(data);
        for (var i = 0; i < json.results.TicketGroups.length; i++) {
            var section = json.results.TicketGroups[i].TicketTypeName;
            var no = json.results.TicketGroups[i].Qty;
            var price = json.results.TicketGroups[i].
            Price;
            var button =
                "<button class='btn btn-info' data-url='LINK'>Click Here</button>";
            $("#tableid").append("<tr><td>seatwave</td><td>" + section +
                "</td><td>N/A</td><td>N/A</td><td>" + no +
                "</td><td>£"+price +
                "</td><td>" + button + "</td></tr>");
            $("#tableid").find(".btn.btn-info").click(function() {
                location.href = $(this).attr("data-url");
            });
        }
        sortTable();
    },
    error: function(error) {
        console.log(error);
    }
});
  • 写回答

2条回答 默认 最新

  • weixin_33697898 2015-02-26 18:27
    关注

    you are selecting all buttons every iteration of the for loop;

    //this line selects all elements with classes .btn.btn-info
    $("#tableid").find(".btn.btn-info")
    

    therefore once its done all your buttons will have the link of the last created button.

    it would be easier if instead of the line:

    var button = "<button class='btn btn-info' data-url='LINK'>Click Here</button>";
    

    you would wrap the button with a jQuery object, and attach the click handler to it:

    var button = $("<button class='btn btn-info' data-url='LINK'>Click Here</button>");
    button.click(function() {
         location.href = $(this).attr("data-url");
    }); 
    

    and when you add the button to the dom you can do it this way:

    //this will retrieve the html element from the jquery object
    "</td><td>" + button[0] + "</td></tr>"  
    
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?