今天做项目时,用到一种用jquery循环获取name相同的按钮,并且完成点击事件,记录一下
首先整段的html,是用js拼出来的(项目需求)
getStudentArticle:function(options,data){ var length = data.length > options.maxNum ? options.maxNum : data.length; console.log(data); var isReceipt = ""; for (var i = 0; i < length; i++) { this.append(` <div class='correction-main-btn-box'> <button class='button button-on' name="corrections" id="`+data[i].id+`">批改</button> </div> );if (data[i].status != 3) { $("button[name=corrections]").eq(i).attr('disabled',true); } }
$("button[name=corrections]").each(function(i,item){ $("button[name=corrections]").eq(i).click(function(e) { e.preventDefault(); var id = $("button[name=corrections]").eq(i).attr('id'); }) });