如何在HTML标签内部将数据传递给函数?

时间:2017-04-26 18:55:49

标签: javascript jquery html

我通过调用一个函数修复了这个问题,该函数抓取了它所放置的表行的id:)

我有这段代码将一些不同的东西排序到一个表中,这很好。问题是将数据传递到HTML标记内部的函数中,如下所示: See reference here

$.when(document, getTournamentsData()).done(function(){
var output = "";
$.each(tournamentsData, function(key, data){
        output += "<tr class='data_row "+data.isOpen+"' id='"+data._id+"'>";
        output += "<td>" + (key+1) + "</td>";
        output += "<td><b>" + data.name + "</b></td>";
        output += "<td>Start: " + data.begintime + "<br>Slut: " + data.endtime + "</td>";
        output += "<td><input class='btn btn-primary' type='button' value='Se beskrivelse' onclick='showTourDescription(data.description)'/></td>";
        output += "<td><input class='btn btn-primary' type='button' value='Se billede' onclick='showPic(data.image)'/></td>";
        output += "<td>Max antal: "+ data.max_teams +"<br>Tilmeldte: "+ data.teams.length +"</td><br>";
        output += "<td><input class='btn btn-primary' type='button' value='Se deltagere' onclick='showMembers(data.teams)'/></td>";
        output += "<td>" + prizes(data.prices) + "</td>";                                                                                               
        output += "</tr>";
});
output += "";
$('#data_insert').append(output);

});

我在函数中所做的就是控制数据,我收到错误“数据未定义”

这是完整的脚本https://github.com/Jakobtottrup/OptekSemester2/blob/master/Web/public/js/tournaments_admin.js

2 个答案:

答案 0 :(得分:0)

更新:看起来您将数据作为字符串传递。看看是否有效

$.each(tournamentsData, function(data) {

onclick='showTourDescription(" + data.description + ")

答案 1 :(得分:0)

// list tournaments
$.when(document, getTournamentsData()).done(function(){
    var output = "";
    $.each(tournamentsData, function(key, data){
            output += "<tr class='data_row "+convertBoolean(data.isOpen)+"' id='"+data._id+"'>";
            output += "<td>" + (key+1) + "</td>";                                                                                                           // index
            output += "<td><b>" + data.name + "</b></td>";                                                                                                  // navn
            output += "<td>Start: " + data.begintime + "<br>Slut: " + data.endtime + "</td>";                                                               // start/slut
            output += "<td><input class='btn btn-primary' type='button' value='Se beskrivelse' onclick='showTourDescription("+data.description+")'/></td>";     // beskrivelse TODO
            output += "<td><input class='btn btn-primary' type='button' value='Se billede' onclick='showPic("+data.description+")'/></td>";                     // billede
            output += "<td>Max antal: "+ data.max_teams +"<br>Tilmeldte: "+ data.teams.length +"</td><br>";                                                 // hold
            output += "<td><input class='btn btn-primary' type='button' value='Se deltagere' onclick='showMembers("+data.teams"+)'/></td>";                     // medlemmer
            output += "<td>" + prizes(data.prices) + "</td>";                                                                                               // præmier
            output += "</tr>";
    });

我已经使用了你的代码,正如你在HTML字符串中所做的那样,也在事件处理程序中连接起来。应该就是你需要做的一切。