最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unable to check uncheck a checkbox using jquery in metronic admin theme - Stack Overflow

programmeradmin4浏览0评论

I am using metronic admin theme for one of my project.

I have to checked or unchecked check boxes on base of data from database but problem is that i am unable to checked or unchecked a check box in metronic theme using jquery. I have tried both ways to acplish this task using prop and attr but nothing is working for me. If I run the same code on my custom web page it's working perfectly fine.

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

I am using metronic admin theme for one of my project.

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

I have to checked or unchecked check boxes on base of data from database but problem is that i am unable to checked or unchecked a check box in metronic theme using jquery. I have tried both ways to acplish this task using prop and attr but nothing is working for me. If I run the same code on my custom web page it's working perfectly fine.

$("#checkbox").prop("checked",true);
$("#checkbox").attr('checked',false);
$("#checkbox").attr('checked','checked');
Share Improve this question edited Apr 4, 2017 at 17:08 Asad asked May 17, 2016 at 5:26 AsadAsad 5087 silver badges21 bronze badges 2
  • Some time location of your code does matter. May be you are placing this code snippet at wrong place. can you show plete code snippet? – Atif Commented May 17, 2016 at 5:43
  • "on my custom web page it's working", whats different from that page and the one its not working on? Are there errors in the console? Is the checkbox a custom element that wraps the actual <input> checkbox element, eg Polymer element? – Patrick Evans Commented May 17, 2016 at 5:44
Add a ment  | 

4 Answers 4

Reset to default 14

If you are using metronic, try adding this line after the prop:

$.uniform.update();

Metronic theme uses uniform library to modify standard form element into fancy element. If you check or uncheck element using jquery, it is updated but it is not reflected on front-end. To update this action on front-end you need to update using uniform library.

$.uniform.update() restyles the element depending on updated action.

Best way for this problem add class for input checkbox when you are using in Angular Js loop.

This works perfectly:

I also use the metroic theme,but no such problems: I think the problem maybe is your 'unchecked' and 'checked' is not matching;my meaning is:you should

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

if you use removeAttr,you should

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

if above all it don't work ,you can try:

$(this)[0].checked=true;// when I use ,find return array,so add [0];
$(this)[0].checked=false;

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

or

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

can be found here

发布评论

评论列表(0)

  1. 暂无评论