更新:
我修正了其中一个问题,我的类名拼写错误.愚蠢的错误!
最后一个问题是必须始终检查一个,所以基本上,除非选择另一个选项,否则他们永远不能取消检查已经检查的选项。
我一直试图解决一个问题与复选框,并只允许一个在任何时候被选中。
基本上,我想:
相关代码如下:
var $boxes = $('#my-details-modal .modal-dialog .modal-content .modal-body form label input[type="checkbox"]');
$boxes.on('click', function(e) {
var $box = $(this);
if ($box.is(":checked")) {
var boxGroup = "input:checkbox[name='" + $box.attr("name") + "']";
$(boxGroup)
.prop("checked", false)
.parent()
.removeClass('checked-checkbox-parent');
$box
.prop("checked", true)
.parent()
.addClass('checked-checkbox-parent');
} else {
$box
.prop("checked", false)
.parent()
.removeClass('checked-checkbox-parent');
}
});.profile-edit-btn {
color: #C99C49;
text-decoration: none;
}
.modal .modal-dialog .modal-content {
border-radius: 0;
}
.modal .modal-dialog .modal-content .modal-header {
background: black;
color: white;
font-family: "museo500";
text-transform: uppercase;
font-weight: bold;
font-size: 20px;
}
.modal .modal-dialog .modal-content .modal-header button {
color: white;
font-family: "museo500";
font-size: 30px;
opacity: 1;
display: block;
}
.modal .modal-dialog .modal-content .modal-body {
background: #F6F6F6;
color: black;
}
#my-details-modal .modal-dialog .modal-content .modal-body form .btn-success {
border-radius: 0;
width: auto;
}
#my-details-modal .modal-dialog .modal-content .modal-body form label,
#my-details-modal .modal-dialog .modal-content .modal-body form input {
width: 100%;
}
#my-details-modal .modal-dialog .modal-content .modal-body form label input[type="checkbox"] {
position: absolute;
top: -9999px;
left: -9999px;
}
#my-details-modal .modal-dialog .modal-content .modal-body form label {
display: block;
background: #C99C49;
margin: 10px 0;
cursor: pointer;
padding: 20px;
}
#my-details-modal .modal-dialog .modal-content .modal-body form label.checked-checkbox-parent {
background: black;
}
#my-details-modal .modal-dialog .modal-content .modal-body form label span {
font-size: 16px;
color: white;
font-family: "museo500";
font-weight: bold;
text-transform: uppercase;
}
#my-details-modal .modal-dialog .modal-content .modal-body form label span:nth-of-type(2) {
font-family: "museo300";
font-weight: normal;
}
#my-details-modal .modal-dialog .modal-content .modal-body form .confirmation-para {
color: black;
font-weight: bold;
font-family: "museo500";
font-size: 14px;
text-transform: uppercase;
}
#my-details-modal .modal-dialog .modal-content .modal-body form input[type="password"] {
display: block;
width: 300px;
font-weight: bold;
font-family: "museo500";
font-size: 14px;
text-transform: uppercase;
padding: 5px;
margin-bottom: 20px;
}<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<a href="#" class="profile-edit-btn" data-toggle="modal" data-target="#my-details-modal">Open Modal</a>
<div class="modal fade" tabindex="-1" role="dialog" id="my-details-modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span>
</button>
<h4 class="modal-title">Update your membership</h4>
</div>
<div class="modal-body">
<form action="#" method="post">
<label for="one-month">
<input id="one-month" name="profile-modal-check" type="checkbox">
<span>Monthly</span>
<span class="pull-right">(£30/case)</span>
<div class="clearfix"></div>
</label>
<label for="three-month">
<input id="three-month" name="profile-modal-check" type="checkbox">
<span>3 Months</span>
<span class="pull-right">(£29/case)</span>
<div class="clearfix"></div>
</label>
<label for="six-month">
<input id="six-month" name="profile-modal-check" type="checkbox">
<span>6 Months</span>
<span class="pull-right">(£28/case)</span>
<div class="clearfix"></div>
</label>
<label for="twelve-month">
<input id="twelve-month" name="profile-modal-check" type="checkbox">
<span>12 Months</span>
<span class="pull-right">(£27/case)</span>
<div class="clearfix"></div>
</label>
<p class="confirmation-para">Enter your password to confirm</p>
<input type="password" name="confirmation-password" placeholder="password">
<input type="sumbit" value="confirm" class="btn btn-success pull-right">
</form>
<div class="clearfix"></div>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
如果你有任何问题,请评论下面,我希望一切都足够清楚。
发布于 2015-12-08 19:33:24
您的代码有许多多余的部分。例如,您选择了所有需要的复选框($boxes),然后在事件处理程序中再次选择它们(boxGroup)。为什么?您可以使用$boxes变量,它已经准确地存储了所需的集合。
此外,您必须侦听change事件,而不是click事件。
对于复选框、复选框和单选按钮,当用户使用鼠标进行选择时,事件将立即触发。
在这种情况下,您不需要检查是否选中了this元素。
因此,代码应该明显地简化。
在事件处理程序中,只需为所有元素设置“禁用状态”道具/类,然后为选中元素设置“启用状态”道具/类。
var $boxes = $('#my-details-modal .modal-dialog .modal-content .modal-body form label input[type="checkbox"]');
$boxes.on('change', function(e) {
$boxes.prop("checked", false)
.parent().removeClass('checked-checkbox-parent');
$(this).prop("checked", true)
.parent().addClass('checked-checkbox-parent');
});发布于 2015-12-09 03:19:42
经过一段时间的思考和重构,这是我最后的结果,它可以工作,可能不是“最干净”的方式,但它做的正是我所能看到的所需要的。
请参阅我在代码中所做的评论,以获得更多信息/在前面编写此代码时查看我的思维过程。
//target all the checkboxes for membership type
var $boxes = $('#my-details-modal .modal-dialog .modal-content .modal-body form label input[type="checkbox"]');
//loop over each one and find the one that is to be checked at the start (determined by the users selected current subscription) and add the checked "state" to it
$boxes.each(function() {
if ($(this).attr("checked")) {
$(this)
.prop("checked", true)
.parent()
.addClass('checked-checkbox-parent');
}
});
//when any of the boxes are clicked
$boxes.on('click', function(e) {
//target the box being clicked on
var $box = $(this);
//if it is checked
if ($box.is(":checked")) {
//target all the checkboxes and reset their checked state to false
$boxes
.prop("checked", false)
.parent()
.removeClass('checked-checkbox-parent');
//then select the current box being selected and set its checked state to true, thus only one box can ever be checked and one must always be checked.
$box
.prop("checked", true)
.parent()
.addClass('checked-checkbox-parent');
}
});.profile-edit-btn {
color: #C99C49;
text-decoration: none;
}
.modal .modal-dialog .modal-content {
border-radius: 0;
}
.modal .modal-dialog .modal-content .modal-header {
background: black;
color: white;
font-family: "museo500";
text-transform: uppercase;
font-weight: bold;
font-size: 20px;
}
.modal .modal-dialog .modal-content .modal-header button {
color: white;
font-family: "museo500";
font-size: 30px;
opacity: 1;
display: block;
}
.modal .modal-dialog .modal-content .modal-body {
background: #F6F6F6;
color: black;
}
#my-details-modal .modal-dialog .modal-content .modal-body form .btn-success {
border-radius: 0;
width: auto;
}
#my-details-modal .modal-dialog .modal-content .modal-body form label,
#my-details-modal .modal-dialog .modal-content .modal-body form input {
width: 100%;
}
#my-details-modal .modal-dialog .modal-content .modal-body form label input[type="checkbox"] {
position: absolute;
top: -9999px;
left: -9999px;
}
#my-details-modal .modal-dialog .modal-content .modal-body form label {
display: block;
background: #C99C49;
margin: 10px 0;
cursor: pointer;
padding: 20px;
}
#my-details-modal .modal-dialog .modal-content .modal-body form label.checked-checkbox-parent {
background: black;
}
#my-details-modal .modal-dialog .modal-content .modal-body form label span {
font-size: 16px;
color: white;
font-family: "museo500";
font-weight: bold;
text-transform: uppercase;
}
#my-details-modal .modal-dialog .modal-content .modal-body form label span:nth-of-type(2) {
font-family: "museo300";
font-weight: normal;
}
#my-details-modal .modal-dialog .modal-content .modal-body form .confirmation-para {
color: black;
font-weight: bold;
font-family: "museo500";
font-size: 14px;
text-transform: uppercase;
}
#my-details-modal .modal-dialog .modal-content .modal-body form input[type="password"] {
display: block;
width: 300px;
font-weight: bold;
font-family: "museo500";
font-size: 14px;
text-transform: uppercase;
padding: 5px;
margin-bottom: 20px;
}<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<a href="#" class="profile-edit-btn" data-toggle="modal" data-target="#my-details-modal">Open Modal</a>
<div class="modal fade" tabindex="-1" role="dialog" id="my-details-modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span>
</button>
<h4 class="modal-title">Update your membership</h4>
</div>
<div class="modal-body">
<form action="#" method="post">
<label for="one-month">
<input id="one-month" name="profile-modal-check" type="checkbox" checked>
<span>Monthly</span>
<span class="pull-right">(£30/case)</span>
<div class="clearfix"></div>
</label>
<label for="three-month">
<input id="three-month" name="profile-modal-check" type="checkbox">
<span>3 Months</span>
<span class="pull-right">(£29/case)</span>
<div class="clearfix"></div>
</label>
<label for="six-month">
<input id="six-month" name="profile-modal-check" type="checkbox">
<span>6 Months</span>
<span class="pull-right">(£28/case)</span>
<div class="clearfix"></div>
</label>
<label for="twelve-month">
<input id="twelve-month" name="profile-modal-check" type="checkbox">
<span>12 Months</span>
<span class="pull-right">(£27/case)</span>
<div class="clearfix"></div>
</label>
<p class="confirmation-para">Enter your password to confirm</p>
<input type="password" name="confirmation-password" placeholder="password">
<input type="sumbit" value="confirm" class="btn btn-success pull-right">
</form>
<div class="clearfix"></div>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
https://stackoverflow.com/questions/34163613
复制相似问题