var $eContainer = $('<div/>', { id: "eContainer", class: 'eList' }).appendTo($injectTo);
for (var i in navProps) {
var np = navProps[i];
var npName = np.name;
var npId = containerId + "_" + npName;
var $eLabel = $('<label />', { 'for': npId, text: npName }).appendTo($eContainer);
$('<input />', { type: 'checkbox', id: npId, value: npName }).prependTo($eLabel);
};
输出
<label for="e_22">
<input type="checkbox" id="selectcolumn_16" value="22">Supplier<div id="eContainer" class="eList">
<label for="a">
<input type="checkbox" id="a" value="A">AAA</label>
<label for="b">
<input type="checkbox" id="b" value="B">BBB</label>
</div>
</label>
问题
-
它不关闭输入标签 - 当我试图让标签的文本它返回所有子标签的文本以及
守则第二个问题
// Event handler for adding labels.
this.$eList.on('click', ':input', $.proxy(function (event) {
var $e = $(event.target);
var selectedLabel = $e.parent().text();
// here it should return "Supplier" when I click on it, but it returns
// SupplierAAABBB -- which is WRONG
编辑
请不要”用硬代码提供解决方案,因为我需要使其动态工作,所以它一定需要从父代获取它,会有几个层次的这些di v程序和控制
在我看来,你的代码是好的,但HTML可能不是你想要的。所有这些文字确实在该标签中。也许你想将'div'移到标签外面呢? – smarx
至于'input'标签,它们不应该被“关闭”。在XHTML中,它们会自动关闭(''),但是在HTML中,它们是无效标记,看起来就像上面那样('')。 – smarx
如果你希望'div'在'label'之后,而不是在里面,可以试试'.insertAfter(...)'而不是'.appendTo(...)'。 – smarx