Internet Explorer(和 jquery)中的标签和隐藏字段 [英] Labels and hidden fields in Internet Explorer (and jquery)

查看:40
本文介绍了Internet Explorer(和 jquery)中的标签和隐藏字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在检查 IE 中的隐藏复选框时遇到问题.这是基本的 html:

I'm having trouble checking hidden checkboxes in IE. This is the base html:

<input id="groups_ids_1" name="group_ids[]" type="checkbox" value="1" />
<label for="groups_ids_1">Display</label>

这很好用,但如果我随后使用任一隐藏复选框

This works fine, but if I then hide the checkboxes using either

$('input[type=checkbox]').hide();

$('input[type=checkbox]').css('visibility', 'hidden');

单击标签不再选中 IE 中的复选框.当然,它在 Firefox、Chrome 和 Safari 中运行良好.

Clicking the label no longer checks the checkbox in IE. Of course it works fine in Firefox, Chrome and Safari.

推荐答案

您可以尝试在标签上添加 onclick 以解决 IE 问题.

You could try added an onclick to the label to get around the IE issues.

$('label').click(function() {
  $('#' + $(this).attr('for')).click();
});

如果这不起作用,请尝试手动设置属性.

If that does not work, try setting the attribute manually.

$('label').click(function() {
  var checkbox = $('#' + $(this).attr('for'));
  if (checkbox.is(':checked')) {
    checkbox.removeAttr('checked');
  } else {
    checkbox.attr('checked', 'checked');
  }
});

这篇关于Internet Explorer(和 jquery)中的标签和隐藏字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆