[英]Get value from dynamically created checkboxes if checked
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.我做了几个复选框和一个按钮,在提交时调用 function 我想获取已选中的复选框的所有值。
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.请建议一些东西来检索生成值的检查值id
是数组形式。 I can not use $("#nearby[0]").val()
.我不能使用$("#nearby[0]").val()
。
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.net/e6Sr3/现场测试用例: http://jsfiddle.net/e6Sr3/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.