20180315_前端_根据一段字符串,将checkbox反显,通常用在修改中
需求:根据一段字符串,将checkbox反显,通常用在修改中。
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(".btn1").click(function(){ var tt = "Bike,Airplane"; var array = tt.split(","); $("input[name='vehicle']").each(function(){ //遍历checkbox for(var i=0;i<array.length;i++){ if($(this).val()==array[i]){ //将checkbox的value与字符数组逐一比较 $(this).attr("checked","checked"); } } }); }); }); </script> </head> <body> <form action=""> I have a bike: <input type="checkbox" name="vehicle" value="Bike" /> <br /> I have a car: <input type="checkbox" name="vehicle" value="Car" /> <br /> I have an airplane: <input type="checkbox" name="vehicle" value="Airplane" /> </form> <button class="btn1">Hide Checkboxes</button> </body> </html>