如何检查复选框是否被选中至少一个在Struts2

How to check whether the checkboxlist is checked atleast one in Struts2?

本文关键字:一个 Struts2 何检查 检查 复选框 是否      更新时间:2023-09-26

我正在struts2框架中工作。我有一个复选框列表在我的代码。

<div id="MyCheckbox">
    <td>
      <s:checkboxlist theme="simple" name="claimListView.claimStatus" list="#{'U':'Unprocessed', 'P':'Processed','R':'Routed','S':'Sent'}" id="claimStatus" required="true" value="defaultColor"/>
    </td>
</div>

所以我希望这个字段是必填字段。我的意思是用户应该选择至少一个复选框。否则,应该抛出错误消息并阻止提交表单。我该怎么做呢?

我在jquery中尝试了下面给出的代码。

var n = $("#MyCheckbox input:checked").length;
alert("The count is : "+n);
if (n>0) {
return true;
}
else{
return false;
}
我知道我错过了什么。谁能告诉我,我哪里出错了?

我终于找到了解决我自己问题的方法。下面的脚本运行正常。

var n = $('input[name="claimListView.claimStatus"]:checkbox:checked').length;
if (n>0) {
return true;
}
else{
return false;
}