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

问题描述

| 我做了几个复选框以及一个在提交时会调用一个按钮的函数,我想在其中获取所有已选中的复选框的值。 用于创建这些复选框的代码是:
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);
}
现在,我想检索已检查的值。我正在尝试(但无法使用)
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);

             }
请提出一些建议以数组形式检索生成值的检查值
id
。我不能使用
$(\"#nearby[0]\").val()
。     

解决方法

        
var checkedCheckBoxesValueArray = $(\'#mapdisplay input:checkbox:checked\').map(
  function(){
    return this.value;
}).get();
    ,        正确的语法为:
$(\'#mapdisplay input:checkbox:checked\').each(function(index) {
    checked_vals.push($(this).val());
});
实时测试案例:http://jsfiddle.net/e6Sr3/