简体   繁体   English

检查是否选中了复选框,而不使用javascript中的变量

[英]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. 我有一个存储复选框ID的变量,但是当我尝试检查复选框是否选中时,它不能正常工作。

   <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. 在您的ID中,但最好避免使用此类ID。

   var id = "checkbox1\\.0";

The demo. 演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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