AUI复选框未显示为已选中但值为true

时间:2014-06-24 07:51:43

标签: javascript html user-interface checkbox yui

我有一个复选框。

<aui:input name = "enable" id = "enable" type="checkbox" label= 'Enable'  onchange='handleClick(this);' />

我在页面加载时加载复选框的上一个状态。检查以前的状态时,我会执行以下操作。

var namespace = "<portlet:namespace/>";
window.onload = function() {
    var cb = document.getElementById(namespace+ "enable");
    cb.checked = true;
    alert("state: " + cb.checked);
    //do something
};

警告显示复选框为true,并执行后面的代码。但是,视图不会显示要检查的复选框!我错过了什么?

1 个答案:

答案 0 :(得分:2)

AUI Input的不好或好处是,它会自动将 Checkbox 附加到input:checkbox的ID。

<input type="checkbox" onchange="handleClick(this);" value="true" onclick="Liferay.Util.updateCheckboxValue(this); " name="_manageorganization_WAR_manageorganizationportlet_enableCheckbox" id="_manageorganization_WAR_manageorganizationportlet_enableCheckbox" class="aui-field-input aui-field-input-choice">

对于您"<portlet:namespace/>";的情况,它会将ID生成为:

namespace+ "enableCheckbox"

因此请将其用作:

var namespace = "<portlet:namespace/>";
window.onload = function() {
    var cb = document.getElementById(namespace+ "enableCheckbox");
         cb.checked = true;
        alert("state: " + cb.checked);
         //do something
}; 

它会起作用。

相关问题