【问题标题】:Checkbox value to change from 0 to 1 when custom span is clicked单击自定义跨度时复选框值从 0 更改为 1
【发布时间】: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
  • 否,未选中的复选框在提交表单时不会传输。

标签: jquery html css checkbox


【解决方案1】:

它在“chrome 版本 29.0.1547.66 m”、“firefox 版本 22.0”中运行良好。但如果在其他浏览器中不起作用,您可以测试以下代码:

JsFiddle Link

$('div.ticks div.col-xs-12 input,div.ticks div.col-xs-12 input label').on('click',function () {
 // this.value = this.checked ? 1 : 0;    
 var checkBox = $(this).find("input:checkbox");
checkBox.value = checkbox.checked ? 1 : 0;    
});

【讨论】:

  • @Himu 你看到 jsfiddle 链接了吗?你可以在那里尝试你的代码。可能问题不在于代码...您的浏览器是否允许运行 javascript?
  • 您好,发现了问题。这是我的 js 文件的问题。谢谢你的帮助。您的代码实际上适用于上面给出的描述。我的 javascript 有问题。
猜你喜欢
  • 1970-01-01
  • 2014-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-30
  • 1970-01-01
  • 1970-01-01
  • 2010-09-16
相关资源
最近更新 更多