我使用 JSF 2.0 创建了 Web 应用程序,其中有许多复选框。我想在单击全选复选框时选中这些复选框。
我有以下代码
<t:selectBooleanCheckbox value="#{UserRegistration.selectAll}" onclick="selectAll(this)" />
<t:selectManyCheckbox value="#{UserRegistration.pagesSelected}" layout="pageDirection" layoutWidth="4">
<f:selectItem itemValue="registerForPatentss" itemLabel="Register New Applicant"/>
<f:selectItem itemValue="success" itemLabel="Register New Project" />
<f:selectItem itemValue="getReportss" itemLabel="Project Reports" />
<f:selectItem itemValue="searhPatentss" itemLabel="Search For Project" />
<f:selectItem itemValue="addUserss" itemLabel="Add User Account" />
<f:selectItem itemValue="logPatentSystemss" itemLabel="System Log" />
<f:selectItem itemValue="userlistss" itemLabel="Details Of Registered Users" />
</t:selectManyCheckbox>
javascript是
<script language="javascript">
function selectAll(checkbox) {
alert('called me == ' + checkbox.checked);
var elements = checkbox.form.elements;
if (checkbox.checked==true) {
alert('selecting all');
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
alert('selecting all ' + element.checked + '==' + element.id + '==');
if (/checkboxId$/.test(element.id)) {
alert('inside...')
element.checked = checkbox.checked;
}
}
}
}
</script>
但是它不起作用。
为了进行测试,我添加了alert('selecting all ' + element.checked + '==');
但是为此我得到了输出,selecting all ====
即我没有得到element.id
.
当我改变<t:selectManyCheckbox
时,<h:selectManyCheckbox
我得到了价值。
知道我做错了什么吗?
编辑 1
HTML 生成的输出是
<table id="favNumber4">
<tr><td><label><input type="checkbox" name="favNumber4" checked="checked" value="1" /> Joomla Websites (Fahim M Parkar)</label></td><td><label><input type="checkbox" name="favNumber4" checked="checked" value="2" /> iPhone Application (Fahim M Parkar)</label></td></tr>
<tr><td><label><input type="checkbox" name="favNumber4" checked="checked" value="3" /> Project 001 (New Applicant Two)</label></td><td><label><input type="checkbox" name="favNumber4" value="4" /> Project 002 (New Applicant Two)</label></td></tr>
<tr><td><label><input type="checkbox" name="favNumber4" value="5" /> Project 003 (New Applicant Two)</label></td><td><label><input type="checkbox" name="favNumber4" value="6" /> Project 004 (New Applicant Two)</label></td></tr>
<tr><td><label><input type="checkbox" name="favNumber4" value="7" /> PP1 (Testing Applicant)</label></td><td><label><input type="checkbox" name="favNumber4" value="8" /> PP2 (Testing Applicant)</label></td></tr>
<tr><td><label><input type="checkbox" name="favNumber4" value="9" /> TA - PR001 (Testing Applicant)</label></td><td><label><input type="checkbox" name="favNumber4" value="10" /> XXXXX 1 (XXXXX)</label></td></tr>
编辑 2
请注意,我还有更多复选框,如下所示。
<h:selectManyCheckbox value="#{UserRegistration.rightSelected}" id="myRight">
<f:selectItem itemValue="add" itemLabel="Add"/>
<f:selectItem itemValue="delete" itemLabel="Delete" />
<f:selectItem itemValue="edit" itemLabel="Edit" />
<f:selectItem itemValue="invite" itemLabel="Invite For Projects" />
</h:selectManyCheckbox>