There is a cycle of processes. For example such . The system may have several studios for each process. What I want to do is save the resulting studios for each process and save them in a single cell array separated by commas. So that later I could take the resulting studios for each process and save them to the database.

My function with which I am trying to save data:

var LISTOBJ = { saveList: function() { $(".output").html(""); var listCSV = []; $(".studio").each(function() { $(this).find("input").each(function() { listCSV.push($(this).text()); }); var values = '' + listCSV.join(',') + ''; $(".output").append("<input type='text' name='studio[]' value='" + values + "' />"); $("#output").append("<p>" + values + "</p>"); console.debug(listCSV); }); } } 

How can I fix this feature to save studios from one process together? In the end, I want to get something like this:

 Array ( [0] => ,APEX, BASECAMP [1] => , CANVAS, ORBIT) 
  • Great! What do you want from us? - Igor
  • by code: how many ".output" elements do you have and how many "#output"? or var listCSV = []; must be inserted inside $(".studio").each(function() { or var values = '' + listCSV.join(',') + ''; ... - render. - Igor
  • @Igor code changed. You can see here the problem that saves only from one process - Yevgeniy Bagackiy
  • you have in html only the first select class has a studio class, put this class to the second select y too - Igor
  • @Igor exactly fixed already. - Yevgeniy Bagackiy

1 answer 1

  1. In your html, only the first select has a class studio , put this class to the second select u too.

  2. In your code, all select studio classes fall into one list.

     var LISTOBJ = { saveList: function() { $(".output").html(""); $("table").has(".studio").each(function() { var listCSV = []; $(this).find(".studio").each(function() { var text = $(this).val(); if (text) { listCSV.push(text); } var values = '' + listCSV.join(',') + ''; $(".output").append("<input type='text' name='studio[]' value='" + values + "' />"); $(".output").append("<p>" + values + "</p>"); }); }); } } 
  • Integrated your code into mine, and also delivered `var values ​​= '' + listCSV.join (',') + ''; $ (". output"). append ("<input type = 'text' name = 'studio []' value = '" + values ​​+ "' />"); $ (". output"). append ("<p>" + values ​​+ "</ p>"); `for the second each function. Everything worked as it should. You can see it here. Thanks for the help. - Yevgeniy Bagackiy
  • @YevgeniyBagackiy And, for sure, this is my puncture, I apologize. Successes in programming. - Igor