我正在尝试按钮的Onclick事件,但它没有显示警报。任何人都可以建议我如何正确处理点击事件。
function sendGetData () {
$.ajax({
type: 'GET',
url: 'Refresh',
success: function (data) {
var trHTML = '';
$.each(data, function (i, item) {
if(data[i].test_progress=="Complete")
{
var buttonVar = ('<button id="btn11">'+"Clicking"+'</button>');
$("#btn11").click(function(){
alert("The paragraph was not clicked."+i);
});
}
else
{
var buttonVar = ('<button id="btn12">'+"Stoping"+'</button>');
$("#btn12").click(function(){
alert("The paragraph was not clicked."+i);
});
}
trHTML += '<tr><td>' + data[i].test_type +
'</td><td>' + data[i].device_id +
'</td><td>' + data[i].make + ' '+data[i].model+
'</td><td>' + data[i].stack_name +
'</td><td>' + data[i].suite_name +
'</td><td>' + data[i].test_progress + '<br>    '+data[i].tests_run+'/'+data[i].total_tests+
'</td><td>' + data[i].start_time +
'</td><td>' + data[i].end_time +
'</td><td>' + buttonVar +
'</td></tr>';
});
$('#test1').append(trHTML);
$('#test').hide();
$('#show').show();
}
});
}
答案 0 :(得分:1)
尝试使用此方法
$(document).on('click', '#btn12', function(event) {
alert("The paragraph was not clicked."+i);
});
答案 1 :(得分:0)
在将元素添加到DOM之前,正在添加click事件的侦听器。这不起作用。
var trHTML = '';
trHTML += '<tr><td>' + data[i].test_type +
'</td><td>' + data[i].device_id +
'</td><td>' + data[i].make + ' '+data[i].model+
'</td><td>' + data[i].stack_name +
'</td><td>' + data[i].suite_name +
'</td><td>' + data[i].test_progress + '<br>    '+data[i].tests_run+'/'+data[i].total_tests+
'</td><td>' + data[i].start_time +
'</td><td>' + data[i].end_time +
'</td><td>' + buttonVar +
'</td></tr>';
$('#test1').append(trHTML);
$.each(data, function (i, item) {
if(data[i].test_progress=="Complete")
{
var buttonVar = ('<button id="btn11">'+"Clicking"+'</button>');
$("#btn11").click(function(){
alert("The paragraph was not clicked."+i);
});
}
else
{
var buttonVar = ('<button id="btn12">'+"Stoping"+'</button>');
$("#btn12").click(function(){
alert("The paragraph was not clicked."+i);
});
}
您只能将Click事件分配给DOM中已有的元素。在$('#test1').append(trHTML);