Java ArrayList to JavaScript Array

พอดีว่าน้องในทีมต้องใช้เลยเขียนเอาไว้ให้ดูครับ เผื่อใครจะเอาไปใช้ เป็น scriptlet ง่ายๆ ครับ

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<%@ page import="java.util.ArrayList" %>


            ArrayList accounts = new ArrayList();


            StringBuffer values = new StringBuffer();
            for (int i = 0; i < accounts.size(); ++i) {
                if (values.length() > 0) {


        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Parse Java ArrayList to JS Array</title>
        <script type="text/javascript">

            function test() {

                // get checkbox names
                var checkboxs = document.getElementsByName('accountNo');

                // get account value from session
                var accountArray = [ <%= values.toString()%> ];

                for(var i=0;i<accountArray.length;i++) {
                    for(var j=0;j<checkboxs.length;j++) {
                        // checked = true if value are equals
                        if(checkboxs[j].value == accountArray[i]) {
                            checkboxs[j].checked = true;


    <body onload="test()">
        Parse Java Array to JS Array <br/><br/>
        <input id="accountNo" name="accountNo" type="checkbox" value="1"/>KK Savings 1<br/>
        <input id="accountNo" name="accountNo" type="checkbox" value="2"/>KK Savings 2<br/>
        <input id="accountNo" name="accountNo" type="checkbox" value="3"/>KK Savings 3<br/>
        <input id="accountNo" name="accountNo" type="checkbox" value="4"/>KK Savings 4<br/>
        <input id="accountNo" name="accountNo" type="checkbox" value="5"/>KK Savings 5<br/>

0 thoughts on “Java ArrayList to JavaScript Array”

Leave a Reply

Your email address will not be published. Required fields are marked *