【问题标题】:How to check whether the checkboxlist is checked atleast one in Struts2?如何检查复选框列表是否在 Struts2 中至少被选中一个?
【发布时间】:2013-04-25 11:49:34
【问题描述】:

我正在使用 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;
}

我知道我错过了一些东西。谁能帮帮我,我哪里出错了?

【问题讨论】:

  • 你可以为你的文件提供演示......
  • 您的代码似乎没问题,您只需将其放在提交事件处理程序上,如果 n=0 则防止默认
  • @RobinLeboeuf 伙计。你能给我一个示例代码吗?
  • 我需要更多代码来帮助你,至少是你的表单 :)

标签: javascript jquery validation struts2 checkboxlist


【解决方案1】:

最后我找到了解决我自己问题的方法。下面的脚本工作正常。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多