使用此代码动态生成按钮并为其分配 id:
function successCBofMissedNamazFromDB(tx, results) {
if (results != null && results.rows.length > 0 && results.rows != null) {
var htmlstring = "";
var temp = 0;
for (var i = 0; i < results.rows.length; i++) {
htmlstring += '<div class="ui-grid-b">';
htmlstring += '<div class="ui-block-a ui-bar-d" style="font-size:x-medium;height:80px;">'
+ prayerName[i] + '</div>';
htmlstring += '<div class="ui-block-b ui-bar-e" style="font-size:x-medium;height:80px;">'
+ results.rows.item(i).missing_prayers + '</div>';
alert(""+performedNamaz[i]);
htmlstring += '<div class="ui-block-c ui-bar-e" style="font-size:x-medium;height:80px;"><input type="number" data-inline="true" data-mini="true" value="'+performedNamaz[i]+'"/><a href="#" data-role="button" data-inline="true" data-icon="plus" id=button'+i+' style="float:right;width:30%;">ADD</a>'
+''+ '</div>';
htmlstring += '</div>';
}
$("#chart1").empty().append(htmlstring).trigger('create');
}
}
使用此代码获取其点击事件中每个按钮的 id:
$('button').live('click', function(){
var btnId = $(this).attr('id');
alert(btnId);
});
这行有语法错误,这就是为什么当我尝试静态分配 id 时它不能正常工作的原因
htmlstring += '<div class="ui-block-c ui-bar-e" style="font-size:x-medium;height:80px;"><input type="number" data-inline="true" data-mini="true" value="'+performedNamaz[i]+'"/><a href="#" data-role="button" data-inline="true" data-icon="plus" id=button'+i+' style="float:right;width:30%;">ADD</a>'
+''+ '</div>';