4

我正在为我的一个项目使用 Metronic 管理主题。

http://themeforest.net/item/metronic-responsive-admin-dashboard-template/4021469

我必须根据数据库中的数据选中或取消选中复选框,但问题是我无法使用 jquery 选中或取消选中 metronic 主题中的复选框。我已经尝试了两种方法来使用 prop 和 attr 来完成这项任务,但没有什么对我有用。如果我在我的自定义网页上运行相同的代码,它工作得非常好。

$("#checkbox").prop("checked",true);
$("#checkbox").attr('checked',false);
$("#checkbox").attr('checked','checked');
4

4 回答 4

13

如果您使用的是 metronic,请尝试在道具后添加此行:

$.uniform.update();

Metronic 主题使用统一库将标准表单元素修改为花哨元素。如果您使用 jquery 选中或取消选中元素,它会更新但不会反映在前端。要在前端更新此操作,您需要使用统一库进行更新。

$.uniform.update() 根据更新的操作重新设置元素的样式。

于 2016-05-17T07:41:03.867 回答
1

当您在 Angular Js 循环中使用时,此问题的最佳方法是为输入复选框添加类。

这完美地工作:

这完美地工作

于 2019-09-05T07:19:49.653 回答
0

$(this).iCheck('uncheck');

或者

$(this).iCheck('check');

可以在这里找到

于 2016-12-13T17:34:46.153 回答
0

我也使用metroic主题,但没有这样的问题:我认为问题可能是你的'unchecked'和'checked'不匹配;我的意思是:你应该

var checked = $(this).is(":checked");
$.each(function() {
  if (checked) {
     $(this).prop("checked", true);//or $(this).attr("checked", true);
  } else {
     $(this).prop("checked", false);//or $(this).attr("checked", false),can't use $(this).removeAttr('checked');
  }
});

如果你使用 removeAttr,你应该

$(this).attr('checked', 'checked');
$(this).removeAttr('checked');

如果最重要的是它不起作用,您可以尝试:

$(this)[0].checked=true;// when I use ,find return array,so add [0];
$(this)[0].checked=false;
于 2016-05-17T07:31:06.310 回答