【问题标题】:Java Spring MVC form:checkboxes - how to know if any were checkedJava Spring MVC form:checkboxes - 如何知道是否有任何被选中
【发布时间】:2016-11-10 23:43:33
【问题描述】:

假设我的 JSP 中有以下行:

<form:checkboxes path="appliedPlayers" items="${suitablePlayers}" itemValue="id" itemLabel="displayName" />

当没有选中任何复选框时,我想禁用表单提交按钮。比如:

$('#checkboxes').change(function() { 
    if (none_are_checked)
        disableBtn();
});

【问题讨论】:

标签: java jquery spring checkbox


【解决方案1】:

Spring 表单标签不支持这一点。您可以查看以下链接以了解支持的属性。

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/spring-form-tld.html#spring-form.tld.checkboxes

可以做的是,您可以使用 jQuery 在客户端处理这种情况(就像您提到的那样)。

<script>
    $(document).ready(function(){
      $('input[name=players]').change(function() { 
       //alert('hello');
       var checkedNum = $('input[name="players[]"]:checked').length;
       if (!checkedNum) {
        // User didn't check any checkboxes
        disableBtn();
       }  
      });
    });
</script>

说明:在上面的代码 sn-p 中,当复选框元素发生变化时,注册的函数会被调用,该函数会计算选中的复选框元素的数量。如果为零,则进入if条件,这是要求。

注意:以上示例假定复选框的 html 属性名称值为players。如果需要,您可以适当地更改您的 jquery 选择器。

信用: https://stackoverflow.com/a/16161874/5039001

【讨论】:

    猜你喜欢
    • 2017-09-14
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多