Generate dynamic check boxes values from the servlet response

I have values coming from database in a servlet response as id and name; and I am storing it in a Map key-value in a servlet.

I wanted to now create a JSP code which generates a checkboxes and show the dynamic list of values.

Servlet code

String sql = "SELECT customerid , c_name from customer";
ResultSet rs = s.executeQuery(sql);
Map<String, String> options = new LinkedHashMap<String, String>();

while (rs.next()) {
options.put(rs.getString("customerid"), rs.getString("c_name");
}

String json = new Gson().toJson(options);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);

JSP Code--

$(document).ready(function () {                           
    $('.btn-checkbox').click(function () {      
    $.get('dataservlet', function (responseJson) {
    var $input = $('#id1');
    var $type = 'checkbox';
        $.each(responseJson, function (key, value) {              
        $('$type').val(key).text(value).appendTo($input); 
    });
    });
});

//JQuery is not generating the right checkboxes, even when the response is in, need help in forming the correct JQuery.

HTML Code--

div id="id1"></div>

example-

I have a record coming as-

customerid c_name
5000       name1

and I needed a checkbox to get generated as:

<input type ="checkbox" id'5000"/>name1 

// so here key is id and value is name1

I needed a check-box with values in it as below image-

UPDATE

Answers


You are doing it totally wrong. Use the following lines. Demo Fiddle

var append = $('<input type="'+$type+'">'+value+'</input>').attr('id',key).val(value);

$($input).append('<br/>').append(append);
  • $type cannot be used as string - $('$type')
  • key is your ID - so you cannot use it using .val().
  • Why are you appending it to the input, you should insert the new checkboxes after input - .after().
  • And your forget to end $(document).ready() with });.
  • Use .change() and not .click() incase of inputs.

So your final JS :

$(document).ready(function () {                           
    $('.btn-checkbox').change(function () {      
        $.get('dataservlet', function (responseJson) {
            var $input = $('#id1');
            var $type = 'checkbox';
            $.each(responseJson, function (key, value) {              
               var append = $('<input type="'+$type+'">'+value+'</input>').attr('id',key).val(value);
               $($input).append('<br/>').append(append); 
            });
        });
    });
});

Need Your Help

how to get screen shot of the entire frame (including the parts clipped out)

java swing

I want to save a screenshot of my entire frame, but my problem is when i try to do this with my current code, if i made the size of my frame smaller, then i can only save the part of the frame thats

Selenium driver: how to test blur?

selenium selenium-firefoxdriver

I'm using selenium 2.24 firefox driver to test a input box's blur event. Currently after i sendKeys to a input box, i let selenium click another area to trigger the input box blur, however i think ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.