单击事件上的Jquery动态按钮不起作用

时间:2014-11-28 13:17:35

标签: jquery

我已经动态地在jquery中创建了按钮,但是这些按钮的单击功能不起作用。 有人可以帮帮我吗?

<div id ="review_div"></div>

var html = "";
if (Variants.length == 0) {
    html += "<tr><th>No Variants have been specified</th></tr>";
} else {
    html += "<tr><th>File Locations </th><th>View</th></tr>";
    for (var i = 0; i < Variants.length; i++) {                    
        html += "<tr><td>" + Variants[i] + "</td><td>" + '<button class="Review_Button" type="button" id="Review_Variants_' + i + '" onclick="myfunction(+Variants[i]+)">View</button>' + "</td></tr>";
    }
}

$('#review_div').html(html);

function  myfunction(var variant) {
    alert ("something");
}

1 个答案:

答案 0 :(得分:2)

替换for循环中的这行代码(缺少Variants [i]周围的引号):

&#13;
&#13;
html += "<tr><td>" + Variants[i] + "</td><td>" + '<button class="Review_Button" type="button" id="Review_Variants_' + i + '" onclick="myfunction('+Variants[i]+')">View</button>' + "</td></tr>";
&#13;
&#13;
&#13;

同时删除&#39; var&#39;从函数参数中,不需要:

&#13;
&#13;
function  myfunction(variant) {
  alert ("something");
}
&#13;
&#13;
&#13;