【发布时间】:2017-12-13 10:44:28
【问题描述】:
我很好奇这是否可能......
从我的搜索中,我在 SO 上找到了this 问题,但它似乎与我的困境有点不同。
对于那些不关心查看帖子的人,有人试图通过按下单选按钮(放置在被替换的同一 div 中)来刷新 div。从答案看来,它是成功的。
我的问题
我正在从事以这种方式设置的项目:
- 单选按钮行,表示他们想要选择一周中的多少天(3 天或 5 天),或仅选择 1 天。
- 如果用户选择
1 day,在不出现的行下会有一个不可见的div。 - 如果用户选择 3 天或 5 天,则在该行下方显示一个不可见的 div,另一行包含一周中的日期。
- 如果用户选择
3 days,他们只能点击其中的 3 天,而其他 4 天则被禁用(这些“天”按钮是复选框) - 如果用户选择
5 days,你猜对了,他们可以选择5天。 - 如果用户随后重新选择
3 days,那么他们必须取消选择所有日期,然后重新选择它们才能强制执行 3 天限制,因为他们已经选择了5 days被选中。 - 选中复选框时,会在其上激活 CSS 样式,然后其颜色会发生变化。
我要做什么
我正在尝试使我的布局以这种方式工作:
- 如果用户选择
5 days,照常营业。 - 如果用户然后选择
3 days,我希望不可见的div(现在可见,因为one day不是选择的选项)刷新自己:我的意思是有CSS样式已移除且所有复选框均未选中。
我的代码
$("document").ready(function() {
$('input[name=timing]').on('change', function() {
if ($(this).prop('id') == "one") {
$('#recurrence_time').html('One Time');
$('.day-selections').hide();
} else if ($(this).prop('id') == "thrice") {
$('#recurrence_time').html('3 Times a Week');
$('.day-selections').show();
} else if ($(this).prop('id') == "once") {
$('#recurrence_time').html('Once a Week');
$('.day-selections').hide();
} else if ($(this).prop('id') == "custom") {
$('#recurrence_time').html('Custom');
$('.day-selections').show();
}
});
});
$(":checkbox[name=day]").change(function() {
if ($('input#thrice').is(':checked')) {
var len = 3;
} else if ($('input#custom').is(':checked')) {
var len = 5;
}
if ($(":checkbox[name=day]:checked").length == len) {
$(':checkbox:not(:checked)').prop('disabled', true);
} else {
$(':checkbox:not(:checked)').prop('disabled', false);
}
});
function updateHighlightRadio() {
var selected = this.parentNode.parentNode.getElementsByClassName("selected")[0];
if (selected) selected.className = selected.className.replace(" selected", "");
this.parentNode.className += " selected";
}
window.onload = function() {
var radios = document.querySelectorAll("input[type=radio]");
for (var i = 0; i < radios.length; ++i) {
radios[i].onchange = updateHighlightRadio;
}
}
$("input[type='checkbox']").change(function() {
$(this).closest("label").toggleClass(" selected");
});
.selected {
background-color: #FF8C00;
color: #FFFFFF;
}
.how-often p {
font-weight: bold;
text-align: left;
display: inline;
float: left;
}
/* button label */
.recurrence {
border-radius: 8px;
padding: 1em;
margin: 0.5em;
border-radius: 5px;
background-color: #ffd199;
color: #FFFFFF;
cursor: pointer;
display: inline-block;
width: 115px;
text-align: center;
font-size: 12px;
}
/* remove radio button */
.recurrence input {
visibility: hidden;
position: absolute;
}
/* color change when hovering */
.recurrence:hover {
background-color: #FF8C00;
}
.days {
border-radius: 8px;
padding: 1em;
margin: 0.5em;
border-radius: 5px;
background-color: #ffd199;
color: #FFFFFF;
cursor: pointer;
display: inline-block;
text-align: center;
font-size: 12px;
width: 75px;
}
/* remove radio button */
.days input {
visibility: hidden;
position: absolute;
}
/* color change when hovering */
.days:hover {
background-color: #FF8C00;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="how-often pricing">
<p>How often?</p>
<label class="recurrence">
<input required id="one" name="timing" type="radio">Once
</label>
<label class="recurrence">
<input id="once" name="timing" type="radio">Once a week
</label>
<label class="recurrence">
<input id="thrice" name="timing" type="radio">3 Times a week
</label>
<label class="recurrence">
<input id="custom" name="timing" type="radio">Custom
</label>
<hr>
</div>
<!--start day selection section-->
<div class="day-selections" style="display: none;">
<p>Which days?</p>
<label class="days">
<input id="mon" name="day" type="checkbox">
Monday
</label>
<label class="days">
<input id="tue" name="day" type="checkbox">
Tuesday
</label>
<label class="days">
<input id="wed" name="day" type="checkbox">
Wednesday
</label>
<label class="days">
<input id="thu" name="day" type="checkbox">
Thursday
</label>
<label class="days">
<input id="fri" name="day" type="checkbox">
Friday
</label>
<hr>
</div>
注意:由于某种原因,在我的 sn-p 中选择按钮后,我无法让按钮保持新颜色...
【问题讨论】:
-
你能发布一个工作的sn-p吗?
-
我在向 sn-p 添加更多代码时由于某种原因不断出错...我将在凌晨发布一个 JSFiddle 以便
hide和show我的代码函数与 CSS 样式一起工作...... @SergeK. -
你为什么要让它变得更复杂,而不是只使用 3 天和 5 天的单独单选按钮?这样如果他们选择了 5 天,然后再次选择 3 天,选中的 5 天复选框将被移除到最初未选中的复选框。
-
你也可以使用 SO sn-p,就像@GerardoBLANCO 通过edit你的帖子所做的那样
-
当我尝试向其中添加更多代码时,我无法让它工作,我收到了一个错误。 @Highdef,它们都是单独的单选按钮..我没有按照您所说的复选框将被取消选中。需要详细说明吗? :)
标签: javascript jquery html css