0

Im building an mobile app check if the username and password to be save and checked on checkbox , as I want to if checked then my "checkboxcs" class will show up the checked images else none. Here is my code:

function checkRPwd(){
    var chbcv = $('#chb_re_pwd').attr('checked');
    if(chbcv=="checked"){
        $('#chb_re_pwd').attr("checked",false);
        $('#chb_re_pwd')[0].checked=false;
        $('.checkboxcs').removeClass("ckbcsselect");
    }else{
        $('#chb_re_pwd').attr("checked","checked");
        $('#chb_re_pwd')[0].checked=true;
        $('.checkboxcs').addClass("ckbcsselect");
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="checkboxcs" href="javascript:checkRPwd();">
    记住密码
    <input id="chb_re_pwd" value="记住密码" type="checkbox" checked="checked">
</a>
Tân
  • 1
  • 15
  • 56
  • 102
Morgan Ng
  • 813
  • 2
  • 10
  • 22
  • What version of jQuery are you using? – AtheistP3ace Mar 02 '17 at 13:31
  • i dint use any jquery , any idea what jquery version should be use? – Morgan Ng Mar 02 '17 at 13:32
  • The question is tagged with jQuery and the code is using the jQuery library mostly, except when you access the DOM element of the jQuery element array. Either way as of jQuery 1.7 I think you should be using `.prop` method to check/set checkbox values using true/false. – AtheistP3ace Mar 02 '17 at 13:33
  • @AtheistP3ace noted , thanks for the info – Morgan Ng Mar 02 '17 at 13:34
  • If you don't use jquery, it would throw error like `$ is undefined` when calling `$('#chb_re_pwd')`. I've edited your question with jquery version 2.1.1. If something went wrong, you can re-edit. – Tân Mar 02 '17 at 13:35
  • are you getting any error? – rach Mar 02 '17 at 13:53
  • if chb_re_pwd checked then my "checkboxcs" class will show up the checked images else none.. You should be calling function on checkbox click event not on checkboxcs click event.. Please refer : https://jsfiddle.net/ep65qaqx/1/ hope it helps. – rach Mar 02 '17 at 13:57

1 Answers1

1

Use jQuery's is() function:

if($('#chb_re_pwd').is(':checked'))