最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Get value from dynamically created checkboxes if checked - Stack Overflow

matteradmin7PV0评论

I made few checkboxes along with a button which on submit calls a function where I want to get all the values of checkboxes which have been checked.

Code for creating those checkboxes is:

for (var loop=0; loop < count; loop++)
{

  var chap  =   document.createElement("input");
  chap.type =   "checkbox";
  chap.value    =   nearby[loop];
  chap.id   =   nearby[loop];

  document.getElementById("mapdisplay").appendChild(chap);
  var nearo = nearby[loop];
  var s     =   document.getElementById("mapdisplay");
  var text  =   document.createTextNode(nearby[loop]);
  s.appendChild(text);
  var br        =   document.createElement('br');
  s.appendChild(br);
}

Now I want to retrieve the values which are checked. I am trying this (but to no available)

function fsearch()
{
    var p       =   x;
    var narr    =   new Array();
    narr=nearby;//a global arr
    var checked_vals = new Array(); 
    $('#mapdisplay input:checkbox:checked').foreach()

             {
              checked_vals.push(this.value);

             }

Please suggest something to retrieve the checked values id of generated values are in array form. I can not use $("#nearby[0]").val().

I made few checkboxes along with a button which on submit calls a function where I want to get all the values of checkboxes which have been checked.

Code for creating those checkboxes is:

for (var loop=0; loop < count; loop++)
{

  var chap  =   document.createElement("input");
  chap.type =   "checkbox";
  chap.value    =   nearby[loop];
  chap.id   =   nearby[loop];

  document.getElementById("mapdisplay").appendChild(chap);
  var nearo = nearby[loop];
  var s     =   document.getElementById("mapdisplay");
  var text  =   document.createTextNode(nearby[loop]);
  s.appendChild(text);
  var br        =   document.createElement('br');
  s.appendChild(br);
}

Now I want to retrieve the values which are checked. I am trying this (but to no available)

function fsearch()
{
    var p       =   x;
    var narr    =   new Array();
    narr=nearby;//a global arr
    var checked_vals = new Array(); 
    $('#mapdisplay input:checkbox:checked').foreach()

             {
              checked_vals.push(this.value);

             }

Please suggest something to retrieve the checked values id of generated values are in array form. I can not use $("#nearby[0]").val().

Share Improve this question edited Jun 12, 2011 at 14:05 asked Jun 12, 2011 at 13:55 John NashJohn Nash
Add a ment  | 

2 Answers 2

Reset to default 4
var checkedCheckBoxesValueArray = $('#mapdisplay input:checkbox:checked').map(
  function(){
    return this.value;
}).get();

Correct syntax would be:

$('#mapdisplay input:checkbox:checked').each(function(index) {
    checked_vals.push($(this).val());
});

Live test case: http://jsfiddle/e6Sr3/

Post a comment

comment list (0)

  1. No comments so far