Javascript jqueryid+;计数

Javascript jqueryid+;计数,javascript,jquery,html,css,Javascript,Jquery,Html,Css,当我动态创建复选框和div时,我希望每个复选框都有不同的id(如id\u 1、id\u 2…) 数组的第一个值被下一个值擦除。 目前,我为数组中的每个值创建复选框: var containerCheckbox = $('#listCheckboxCategories'); // var listCheckboxCategories = $('#listCheckboxCategories'); var CheckboxCreate = '<input id="catCheckbox" ty

当我动态创建
复选框
div
时,我希望每个复选框都有不同的
id
(如id\u 1、id\u 2…)

数组的第一个值被下一个值擦除。 目前,我为数组中的每个值创建
复选框

var containerCheckbox = $('#listCheckboxCategories');
// var listCheckboxCategories = $('#listCheckboxCategories');
var CheckboxCreate = '<input id="catCheckbox" type="checkbox" name="categoriesCheckbox" required/>';
var categoriesName = '<span id="catName"/>';

if (tileConfig.isThereFilterRadios == "Yes" && res !== undefined) {

  $('#ShowCategories').show();
  $('#containerCategories').show();

  $.each(res.list, function(index, cat) {
    //ToDo: inserer cat.name dans le span
    // categoriesName.html(cat.name)

    containerCheckbox.append(CheckboxCreate, categoriesName);
    $("#catName").html(cat.name);
  });
}
var containerCheckbox=$('listCheckboxCategories');
//var listCheckboxCategories=$(“#listCheckboxCategories”);
var CheckboxCreate='';
变量分类名称=“”;
如果(tileConfig.isThereFilterRadios==“是”&&res!==未定义){
$('#ShowCategories').show();
$(“#集装箱分类”).show();
$.each(资源列表、功能(索引、目录){
//ToDo:inserer cat.name dans le span
//categoriesName.html(cat.name)
containerCheckbox.append(CheckboxCreate,categoriesName);
$(“#catName”).html(cat.name);
});
}

有人能帮我吗?

您可以创建一个函数来返回checkbox元素,这样您就可以将一个变量传递到函数(例如index)中,添加到html中,使每个id都是唯一的

比如说

createCheckbox = function (index) {
 return '<input id="catCheckbox_' + index + '" type="checkbox" name="categoriesCheckbox" required/>';
}

var containerCheckbox = $('#listCheckboxCategories');
var categoriesName = '<span id="catName"/>';

if (tileConfig.isThereFilterRadios == "Yes" && res !== undefined) {

  $('#ShowCategories').show();
  $('#containerCategories').show();

  $.each(res.list, function(index, cat) {
    containerCheckbox.append(createCheckbox(index), categoriesName);
    $("#catName").html(cat.name);
  });
}
createCheckbox=函数(索引){
返回“”;
}
var containerCheckbox=$(“#listCheckboxCategories”);
变量分类名称=“”;
如果(tileConfig.isThereFilterRadios==“是”&&res!==未定义){
$('#ShowCategories').show();
$(“#集装箱分类”).show();
$.each(资源列表、功能(索引、目录){
containerCheckbox.append(createCheckbox(index),categoriesName);
$(“#catName”).html(cat.name);
});
}

您可以创建一个函数来返回checkbox元素,这样您就可以将一个变量传递到函数中(例如index),添加到html中,使每个id都是唯一的

比如说

createCheckbox = function (index) {
 return '<input id="catCheckbox_' + index + '" type="checkbox" name="categoriesCheckbox" required/>';
}

var containerCheckbox = $('#listCheckboxCategories');
var categoriesName = '<span id="catName"/>';

if (tileConfig.isThereFilterRadios == "Yes" && res !== undefined) {

  $('#ShowCategories').show();
  $('#containerCategories').show();

  $.each(res.list, function(index, cat) {
    containerCheckbox.append(createCheckbox(index), categoriesName);
    $("#catName").html(cat.name);
  });
}
createCheckbox=函数(索引){
返回“”;
}
var containerCheckbox=$(“#listCheckboxCategories”);
变量分类名称=“”;
如果(tileConfig.isThereFilterRadios==“是”&&res!==未定义){
$('#ShowCategories').show();
$(“#集装箱分类”).show();
$.each(资源列表、功能(索引、目录){
containerCheckbox.append(createCheckbox(index),categoriesName);
$(“#catName”).html(cat.name);
});
}

谢谢,它返回了所有值,但我仍然有id的问题。(它在第一个复选框之后返回)尝试在每次执行操作时使用递增逻辑来递增一个值,然后将该值连接到您的id。这样每次都会给它们一个不同的id。试试@Martin的建议,或者您可以使用一些工具库,如
下划线.js
?除了
..uniqueId
()之外,它还提供了许多其他实用程序,您可以在将来使用它们。请记住,id并不是访问元素的唯一方式,无论是通过脚本还是CSS。根据您认为需要这些ID的原因,您可能实际上并不需要它们;-)谢谢,它返回了所有的值,但我仍然有id的问题。(它在第一个复选框之后返回)尝试在每次执行操作时使用递增逻辑来递增一个值,然后将该值连接到您的id。这样每次都会给它们一个不同的id。试试@Martin的建议,或者您可以使用一些工具库,如
下划线.js
?除了
..uniqueId
()之外,它还提供了许多其他实用程序,您可以在将来使用它们。请记住,id并不是访问元素的唯一方式,无论是通过脚本还是CSS。根据您认为需要这些ID的原因,您可能实际上并不需要它们;-)