1 回答

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊
報(bào)錯(cuò)誤的原因是,你拼接html的時(shí)候,把title作為一個(gè)變量傳進(jìn)去了,而不是一個(gè)字符串。
而如果你使用字符串冒號(hào)包裹title的話(huà)又會(huì)和html的冒號(hào)沖突,因此比較好的方式是,onclick=cancelDouble('+item.index+',"'+item.title+'") 的時(shí)候,onclick=后面的方法不要用引號(hào)包裹,然后參數(shù)就可以友好的拼接字符串,把參數(shù)當(dāng)成字符串處理而不是變量
var arr = [
{ index: 0, title: 'title0' },
{ index: 1, title: 'title1' },
{ index: 2, title: 'title2' },
{ index: 3, title: 'title3' },
{ index: 4, title: 'title4' },
];
var htm = '';
arr.forEach(function(item) {
htm+='<li><input type="button" value="確定" class="verifition" onclick=cancelDouble('+item.index+',"'+item.title+'")></li>';
});
$("body").html(htm);
function cancelDouble(index,title){
console.log(index,title)
}
添加回答
舉報(bào)