【发布时间】:2016-01-11 00:27:34
【问题描述】:
我有一个烧瓶模板,它为我在模式窗口中加载表单。该表单有 3 个下拉菜单,可以很好地与 required 属性配合使用,但它也有两个“组”复选框。在每个集合中,用户必须选择一个。我是 javascript 新手,似乎无法正常工作。日期和操作必须有单独的 id 才能使 css 与我们拥有的特殊复选框一起正常工作。如何强制我的用户选择一项操作和一天?
编辑 是的,目标是让某人能够检查多天,或两个操作的两个值。我的 css 很糟糕,所以如果有人有办法用多个类来完成这个,那么我当然可以进行编辑。
<head>
<link rel="stylesheet" type="text/css" href="main.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript" language="JavaScript">
function checkCheckBoxes(theForm) {
if (
theForm.m.checked == false &&
theForm.t.checked == false &&
theForm.w.checked == false &&
theForm.th.checked == false &&
theForm.f.checked == false &&
theForm.s.checked == false &&
theForm.sn.checked == false &&)
{
alert ('You didn\'t choose a day!');
return false;
} else {
return true;
}
}
</script>
</head>
<body>
<div class="box" title="Schedule">
<div class="wrapper">
<a data-popup="On demand" href="#refresh" value="Refresh Now">Refresh Now</a>
</div>
<a href="#schedule" value="Change Schedule">Change Schedule</a>
</div>
<aside class="modal" id="schedule">
<h2 >Refresh Schedule</h2>
<section >
<div class="onoffswitch">
<form name="onoff" method="post" action="{{ url_for('togglebool') }}">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="onoffswitch" onchange="this.form.submit()" {{ checkval }}>
<label class="onoffswitch-label" for="onoffswitch">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</form>
</div>
<p id="schedule-mssg"><strong>When should this occur?</strong></p>
<form method='POST' name="schedule_time" action="{{ url_for('info') }}" onsubmit="return checkCheckBoxes(this);">
<select class="custom-dropdown" name="hour" id="hour" required>
<option value="" selected disabled>Hour</option>
{% for i in hours %}
<option value="{{ i }}">{{ i }}</option>
{% endfor %}
</select>
<select class="custom-dropdown" name="minute" id="minute" required>
<option value="" selected disabled>Minute</option>
{% for i in minutes %}
<option value="{{ i }}">{{ i }}</option>
{% endfor %}
</select>
<select class="custom-dropdown" name="period" id="period" required>
<option value="" selected disabled>AM/PM</option>
<option>AM</option>
<option>PM</option>
</select>
<!--Actions, at least one must be checked-->
<input type="checkbox" name="action" id="code" class="css-checkbox" value="code" checked><label for="code" class="css-label">Code</label>
<input type="checkbox" name="action" id="database" class="css-checkbox" value="database" checked><label for="database" class="css-label">Database</label>
<div id="schedule-form">
> <!--Days, at least one must be checked in addition to at least one action-->
<input type="checkbox" name="m" id="m" class="css-checkbox" value="1" checked><label for="m" class="css-label">Mon</label>
<input type="checkbox" name="t" id="t" class="css-checkbox" value="2" checked><label for="t" class="css-label">Tues</label>
<input type="checkbox" name="w" id="w" class="css-checkbox" value="3" checked><label for="w" class="css-label">Wed</label>
<input type="checkbox" name="th" id="th" class="css-checkbox" value="4" checked><label for="th" class="css-label">Thur</label>
<input type="checkbox" name="f" id="f" class="css-checkbox" value="5" checked><label for="f" class="css-label">Fri</label>
<input type="checkbox" name="s" id="s" class="css-checkbox" value="6" checked><label for="s" class="css-label">Sat</label>
<input type="checkbox" name="sn" id="sn" class="css-checkbox" value="7" checked><label for="sn" class="css-label">Sun</label>
</fieldset>
</div>
<input type="submit" name="sched-sub" id="schedule-sub" value="Save Changes"; />
<a href="#" class="btn">Close</a>
</form>
</section>
<footer class="cf">
</footer>
</section>
</aside>
</body>
复选框的 CSS
/* Begin Checkboxes */
input[type=checkbox].css-checkbox {
position:absolute;
z-index:-1000;
left:-1000px;
overflow: hidden;
clip: rect(0 0 0 0);
height:1px;
width:1px;
margin:-1px;
padding:0;
border:0;
}
input[type="checkbox"].css-checkbox + label.css-label { padding-left: 27px;
height: 22px;
display: inline-block;
line-height: 22px;
background-repeat: no-repeat;
background-position: 0px 0px;
font-size: 120%;
margin: 20px;
vertical-align: middle;
cursor: pointer;
font-family: 'Open Sans', sans-serif;
}
input[type=checkbox].css-checkbox:checked + label.css-label {
background-position: 0 -22px;
}
label.css-label {
background-image:url(http://csscheckbox.com/checkboxes/u/csscheckbox_cc5c7a8c1727e75f2de9c422739d0ad5.png);
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* End Checkboxes */
【问题讨论】:
-
所以你想要一些东西来检查客户是否至少选中了一个复选框?如果是这样,您可以在输入元素中添加另一个类名吗?
-
不太确定,我改编了别人的css。我对 css 不是很好,可以添加第二个类并用于区分两个“集合”。
-
如果你给你的元素添加另一个类名,它不会导致css出现问题。当您使用它们来标识不同的元素组时,元素可以有多个类名。我在下面的答案有 2 个类名。看看javascript是否有帮助。如果它不只是留下评论解释为什么它不适合您或显示错误,以便我可以尝试理解问题。
标签: javascript jquery flask