Javascript 禁用提交按钮,直到选中至少一个复选框
我有一个foreach循环来生成复选框,我想确保submit按钮保持禁用状态,直到用户选中至少一个复选框。但即使选中复选框,该按钮仍保持禁用状态 : 这是我的jsp文件Javascript 禁用提交按钮,直到选中至少一个复选框,javascript,jquery,spring-mvc,checkbox,Javascript,Jquery,Spring Mvc,Checkbox,我有一个foreach循环来生成复选框,我想确保submit按钮保持禁用状态,直到用户选中至少一个复选框。但即使选中复选框,该按钮仍保持禁用状态 : 这是我的jsp文件 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transition
<%@ 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>
$('#myform输入:复选框')。更改(函数(){
var a=$('#myform input:checked').filter(“:checked”).length;
如果(a==0){
$('.btn').prop('disabled',true);
}否则{
$('.btn').prop('disabled',false);
}
});
身体{
背景:#中交会;;
!
重要的;
}
/*添加!重要信息:强制浏览器覆盖引导应用的默认样式*/
决定
你好!
浏览所有未解决的问题并选择解决
一些:
问题Id
描述
地位
断然的?
${listValue.ticketId}
${listValue.description}
${listValue.status}
决定
您必须将所有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>
$(文档).ready(函数(){
$('#myform输入:复选框')。更改(函数(){
var a=$('#myform input:checked').filter(“:checked”).length;
如果(a==0){
$('.btn').prop('disabled',true);
}否则{
$('.btn').prop('disabled',false);
}
});
});
这应该可以做到:
$(function(){
$("#myform").on("change", "input", function(){
status=($("#myform").find("input:checked").length==0)?"disabled":"";
$("button").prop("disabled", status);
})
})
这是要玩的。您缺少
$(文档)。准备好了(…
我一直在尝试,但直到现在都没有成功。是这样做的吗?$('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);}}}$(document).ready();@SidNoob,在选中一个复选框后,您是否尝试单击Resolve
按钮?是的。但是我无法单击它,因为它被禁用了!我的操作如下:$(function(){$(“#myform”)。on(“更改”,“输入”,function(){status=($(“form”).find(“input:checked”).length==0?“disabled”:“;$”(“button”).prop(“disabled”,status);})即使选择了一些checkboxestry$(“#myform”).find(“input:checked”).length==0),提交按钮仍然被禁用。或者查看当前版本中的小提琴。现在应该可以工作了。感谢您的响应。还没有好运!