我有一个jQuery脚本,它将显示特定的div,具体取决于选择了哪个选项。

以下是代码:

jQuery.noConflict();
jQuery(document).ready(function() {
  jQuery.viewMap = {
    '' : jQuery([]),
    '1' : jQuery('.company_1'),
    '2' : jQuery('.company_2')
  };
  jQuery('#companyid').change(function() {
    jQuery.each(jQuery.viewMap, function() { this.hide(); });
    jQuery.viewMap[jQuery(this).val()].show();
  });
});

(示例div)

<div class="company_1" style="display: none;">
  <input type="checkbox" name="classifications[Miner]" id="classifications[Miner]" /> Spec/Rough
  <input type="checkbox" name="classifications[Dealer]" id="classifications[Dealer]" /> Dealer
</div>

如果有人选择其他选项,我希望清除div(.company_1,.company_2等)中的所有复选框。如果这有道理? :)

谢谢!

有帮助吗?

解决方案

取消选中复选框是通过删除名为“已检查”的属性来实现的。尝试:

jQuery.each(jQuery.viewMap, function() {
    this.hide();
    jQuery('input:checkbox', this).removeAttr('checked');
});

其他提示

@David Andres 感谢你指出我正确的方向,我无法让你的代码工作,但使用:

$("div.company_1 input:checked").removeAttr("checked");

确实有效。

怎么样:

$("div.company_1 input[type:checkbox]").removeAttr("checked");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top