javascript - 禁用提交按钮,直到至少选中一个复选框

标签 javascript jquery spring-mvc checkbox

我有一个 foreach 循环来生成复选框,我想确保提交按钮保持禁用状态,直到用户选中至少一个复选框。但即使选中复选框后,该按钮仍处于禁用状态。

enter image description here :

这是我的jsp文件

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<link rel="stylesheet" href="bootstrap.css" />
<script
    src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.js"></script>

<link rel="stylesheet"
    href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script
    src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<script>
    $('#myform input:checkbox').change(function() {
        var a = $('#myform input:checked').filter(":checked").length;
        if (a == 0) {
            $('.btn').prop('disabled', true);
        } else {
            $('.btn').prop('disabled', false);
        }
    });
</script>

<style type="text/css">
body {
    background: #CCFFCC;
    !
    important;
}
/* Adding !important forces the browser to overwrite the default style applied by Bootstrap */
</style>

<title>Resolve</title>
</head>
<body>
    <h2>Hi There!</h2>
    <h3>Browse through all unresolved issues and choose to resolve
        some:</h3>
    <div style="background-color: transparent; !important"
        class="table-responsive">
        <div class="btn-toolbar pull-right">
            <div class='btn-group'>
                <a href="back"><button class="btn btn-primary btn-lg">Back</button></a>
            </div>
        </div>
        <table class="table">
            <thead>
                <tr>
                    <th>Issue Id</th>
                    <th>Description</th>
                    <th>Status</th>
                    <th>Resolved?</th>
                </tr>
            </thead>

            <form:form class="form-horizontal" modelAttribute="ticketForm"
                action="${pageContext.request.contextPath }/contact/resolveissue"
                method="get" id="myform">

                <c:if test="${not empty form}">

                    <c:forEach var="listValue" items="${form}">
                        <tr>
                            <td>${listValue.ticketId}</td>
                            <td>${listValue.description}</td>
                            <td>${listValue.status}</td>
                            <td>
                                <div class="col-lg-10">
                                    <input type="checkbox" name="resolve"
                                        value="${listValue.ticketId}" class="form-control">
                                    <form:errors path="resolve"></form:errors>
                                </div>
                    </c:forEach>
                </c:if>

                <div class="btn-toolbar pull-right">
                    <div class='btn-group'>
                        <button type="submit" class="btn btn-primary btn-lg"
                            disabled="disabled">Resolve</button>
                    </div>
                </div>




                <!--                <button type="submit" class="btn btn-primary btn-lg center-block">Resolve</button>
 -->
            </form:form>


        </table>

    </div>

</body>
</html>

最佳答案

您必须将所有的 js/jquery 放入 ready 方法中,如下所示:

<script type="text/javascript">
$(document).ready(function(){
    $('#myform input:checkbox').change(function() {
        var a = $('#myform input:checked').filter(":checked").length;
        if (a == 0) {
            $('.btn').prop('disabled', true);
        } else {
            $('.btn').prop('disabled', false);
        }
    });


});</script>

关于javascript - 禁用提交按钮,直到至少选中一个复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35541816/

相关文章:

javascript - 我可以在本地构建,但我的 heroku 构建失败

javascript - 如何将javascript对象变量转换为类

javascript - 在对象中存储文本字段的值,Javascript

javascript - 无法加载静态资源 :Spring MVC

javascript - 使用javascript上传图片到服务器

JavaScript 函数不检索数组元素

javascript - Google Map Api - 弹出窗口仅在一个点上打开,而在其他点上则不打开

javascript - 如何在每个循环中等待一个 promise ?

java - Spring Web 应用程序被初始化了两次

java - spring boot 从不同的 maven 模块读取属性文件