在前端中,往往需要根據后台數據的返回選中多選框。可以根據后台返回的數據轉化為數組,然后又val([數組])進行選中。
例子:
html代碼:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>多選框選中狀態</title> 5 <script type="text/javascript" src="jquery.js"></script> 6 </head> 7 <body> 8 <input type="checkbox" name="" value="1" class="checkxh"><label>1</label> 9 <input type="checkbox" name="" value="2" class="checkxh"><label>1</label> 10 <input type="checkbox" name="" value="3" class="checkxh"><label>1</label> 11 <input type="checkbox" name="" value="4" class="checkxh"><label>1</label> 12 <input type="checkbox" name="" value="5" class="checkxh"><label>1</label> 13 </body> 14 </html>
javascript代碼:
1 <script type="text/javascript"> 2 $('.checkxh').val([1,2,5]); 3 </script>
得到的效果:

