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');
  }
});