我正在创建一个 jquery 插件以在运行时附加事件和事件处理程序:
的HTML:
<table id="digitalInputsTable">
<thead>
<th>Name</th>
<th>Value</th>
<th>State</th>
<th>Enabled</th>
<th>Polarity</th>
<th>Ignore Reset</th>
</thead>
<tbody>
<tr>
<td>
<span class="">Name1</span>
</td>
<td>
<span class="">0</span>
</td>
<td>
<span class="">Normal</span>
</td>
<td>
<input type="checkbox" value="true" class="" jqmon="0" jqreg="0" id="digitalInputsTable_checkbox_03">
</td>
<td>
<select class="" jqmon="0" jqreg="0" id="digitalInputsTable_select_04">
<option value="0=Alarm">0=Alarm</option>
<option value="1=Alarm">1=Alarm</option>
</select>
</td>
<td>
<input type="checkbox" value="true" class="" jqmon="0" jqreg="0" id="digitalInputsTable_checkbox_05">
</td>
</tr>
</tbody>
数据结构:
$("#digitalInputsTable").bindEvents({
parentControl: "digitalInputsTable",
controls: [
{
id: undefined,
name: "checkbox",
idStartsWith: "digitalInputsTable_checkbox_",
event: "click",
callbackHandler: "checkboxCallback"
},
{
id: undefined,
name: "checkbox",
idStartsWith: "digitalInputsTable_checkbox_",
event: "blur",
callbackHandler: "blurCallback"
},
{
id: undefined,
name: "select",
idStartsWith: "digitalInputsTable_select_",
event: "change",
callbackHandler: "selectCallback"
}
]
});
插件:
$.fn.bindEvents = function(options) {
for(var i = 0; i < options.controls.length; i++) {
var control = options.controls[i];
switch(control.name) {
case "checkbox":
control.name = ":checkbox";
break;
}
if(control.id) {
// find control by ID.
// Attach event to the control.
}
else if(control.idStartsWith) {
$("#" + options.parentControl).find(control.name + "[id^=" + control.idStartsWith + "]")
.bind(control.event, function(e) {
function(ctrl) {
eval(ctrl.callbackHandler + "();");
};
}(control));
}
};
};
问题:
勾选复选框时,无法调用复选框的“checkboxCallback”回调函数。可能是我错误地使用了闭包。请指教。