简体   繁体   中英

checking if checkbox is checked or not using variable in javascript

I have a variable where I have stored my checkbox id's but while I am trying to check if the checkbox is selected or not, it's not working fine.

   <script>
   function check(){
       var id = "checkbox1.0";
       if($('#' + id).is(':checked')){alert("checked");} 
   }
   </script>
   <body>
    <input type ="checkbox" id = "checkbox1.0">
    <input type ="checkbox" id = "checkbox2.0" onclick="check()">
   </body>

You have to escape the . in your id, but you'd better avoid such ids.

   var id = "checkbox1\\.0";

The demo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM