如果选中,则从动态创建的复选框中获取值

2 javascript jquery

我做了几个复选框以及一个按钮,在提交时调用一个函数,我想获取已经检查过的复选框的所有值.

创建这些复选框的代码是:

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);
}
Run Code Online (Sandbox Code Playgroud)

现在我想检索被检查的值.我正在尝试这个(但没有可用)

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);

             }
Run Code Online (Sandbox Code Playgroud)

请建议检索id生成值的选中值的数据以数组形式.我不能用$("#nearby[0]").val().

Jis*_*A P 5

var checkedCheckBoxesValueArray = $('#mapdisplay input:checkbox:checked').map(
  function(){
    return this.value;
}).get();
Run Code Online (Sandbox Code Playgroud)