Get multiple checkbox tag values

Asked

Viewed 267 times

1

I am displaying a table with data coming from a xml, for this I am going through the array @ag and assembling the elements of each line. Each line has a check_box_tag to select the elements to be excluded.

Now what I need is to each selected line extract two elements, that would be identification code and date, because delete I need to request with these two data. The problem is that I am unable to extract two columns by clicking on check_box_tag.

I have a javascript function to store the selected elements in an array I would also have to change this function to store in a multidimensional array. My main question now is how to extract these two columns by clicking on check_box_tag.

Loop to display content from xml:

<% @ag.each do |x|  %>
    <tr>
        <td align="center">
        <%= check_box_tag "#{x[0]}", 0, false, :name=> "chk_del", :onclick => "MultiSelectIDs('chk_del');" %>
         </td>
        <% x.each do |y|  %>  
            <td align="center"><%= y%></td>
            <% end %>
        </tr>
    <% end %>

<%= text_field_tag :selected_ids, "", :size=>100 %>

javascript function:

// Insere na textbox especificada em fieldname os ids selecionados na listagem através dos checkbox
function MultiSelectIDs(FieldName) {
    var selected_ids = new Array()
    var objCheckBoxes = document.getElementsByName(FieldName);
    for(var i = 0; i < objCheckBoxes.length; i++){
        if (objCheckBoxes[i].checked) {
            var x = selected_ids.splice(selected_ids.length,0, objCheckBoxes[i].id); 
        }
    }
    document.getElementById('selected_ids').value = selected_ids.join(", ")
};

Thank you!

  • 1

    Hi, Alessandra, welcome to [pt.so]. It would be nice if you put an example of the generated HTML and maybe create a demonstrative Jsfiddle. You can [dit] the question whenever you have new information to add.

1 answer

0

Let’s imagine that you have the data with the following structure (I will use json for being simpler to write):

json [{ "id": 2, "name": "xpto", "date": "2014-11-11" }, { "id": 3, "name": "xpto2", "date": "2014-11-12" }, { "id": 4, "name": "xpto3", "date": "2014-11-14" }]

If you don’t have a specific format of how you should submit the date and identifier in the request to delete, you can do it without needing any javascript, just by assembling your checkbox to put both information:

You can place your checkbox with the name property with a value similar to elements[<id>][<date>]. Or even put elements[<date>][][id].

In both cases your application will receive a multidimensional array with all the information you need.

If you need a specific format to request, let us know and we can review. If you need examples of how to get this information, indicate which language you use in the backend and we can provide some code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.