【发布时间】:2013-09-19 03:39:22
【问题描述】:
我正在尝试在我正在构建的表单中使用自定义复选框样式here
当我单击复选框的标签时,复选框的值从 0 变为 1,反之亦然。但是,如果我单击包含自定义图像样式的跨度框本身,则该值不会改变。下面是一个 HTML 示例和我用来更改它的 jQUery 代码。我也希望在单击跨度时更改该值。
HTML:
<div class="col-xs-12 tick1">
<span class="checkbox" style="background-position: 0px -48px;"></span>
<input type="checkbox" name="custom9" id="custom9" class="styled">
<label for="custom9">Have you received your entire order from Plush?
<span class="mandatory tick1Mandatory"> * </span> </label>
</div>
JS:
$('#custom9').on('change', function () {
this.value = this.checked ? 1 : 0;
}).change();
【问题讨论】:
-
fyi,如果未选中复选框,则复选框的值无关紧要,因为它从未提交过。
-
复选框的值在 sumbit 期间被传递,因此需要在选中时将其更改为 1
-
否,未选中的复选框在提交表单时不会传输。