6

I am building a list of checkbox items in jQuery based on an array returned from a handler.

The containing element is the .listContainer element below and then each dynamic element I want to add to this takes the form of the .listContainerItem element. Each item will have checkbox value and label based on the array item creating it.

<div class="listContainer">
    <div class="listContainerItem">
        <input type="checkbox" value="1" />
        <div class="listContainerItemLabel">Checkbox 1</div>
    </div>
</div>

At the point of the function that has the array passed to it, what is the most efficient way of creating this? I have been trying to accomplish it as below, but quickly running into major performance issues.

function AddItemToListContainer(item) {
    var currentItems = $j("#listContainerAvailable .listContainerItem");
    if ($j.grep(currentItems, function (e) { return $j(e).find(":checkbox:first").val() == item.Id; }).length === 0) {
        labelDiv = $j("<div />").addClass("listContainerItemLabel").html(item.Text);
        itemToAdd = $j("<div />").addClass("listContainerItem").append("<input type=\"checkbox\" value=\"" + item.Id + "\" />").append(labelDiv);
        currentItems.append(itemToAdd);
    }
}

I've looked at .map function, but not sure quite how to implement it. Can someone point me in the right direction?

4

4 回答 4

1

你想让这些元素一次出现一个吗?或者只是在页面加载时从数组中动态创建?

最后:

html

    <div class="listContainer">
       <div class="listContainerItem"></div>
     </div>  

和jQuery

var array = ['1', '2', '3', '4', '5'];

 $.each(array, function (index, value) {
    $(".listContainerItem").append('<input type="checkbox" value="' + value + '" /> <div         class="listContainerItemLabel">Checkbox ' + value + '</div>');
 });

http://jsfiddle.net/jeremythuff/HEKjk/

一些 .click 事件可以给你第一个效果

于 2013-03-05T04:33:27.627 回答
1

当按下代码的按钮时,它会检查输入字段是否已被填写,如果它是空的,它会弹出一个提醒你如果它不是空的,它会获取输入字段的值,创建一个复选框并将复选框旁边的输入值。

表单的 HTML

<form name="formName" id="listContainerItem">
    <input id="newinput" type="text" value=""/><button id="button">Add new rule</button> <br />
    <input type="checkbox" value="1" /><span class="listcontainerItemLabel" />Checkbox 1 <br />
</form>

Javascript

$(#button).click(function() {
function addLine(e) {
        e.preventDefault();
        var x = document.getElementById('newinput').value;
        if(x == '') {
            alert('not filled in')
        } else {
            $('#listContainer').append('<input type="checkbox" value="1" /><span class="listcontainerItemLabel" />'+x+  '<br />')
        }
    }

});

记住document.getElementById('newinput')也可以这样写:$('#newinput')

您所要做的就是根据您的需要更改将附加的内容。

于 2013-03-04T14:26:44.577 回答
1

我会从这样的事情开始:

var $container = $j('#listContainerAvailable');
var checkboxes = {};

function AddItemToListContainer(item) {
    if (checkboxes[item.Id]) return false;

    checkboxes[item.Id] = true;

    var $item = $j('<div />', {
        'class': 'listContainerItem',
    }).appendTo($container);

    $j('<input />', {
        'type': 'checkbox',
        'value': item.Id
    }).appendTo($item);

    $j('<div />',  {
        'class': 'listContainerItemLabel',
        'text': item.Text
    }).appendTo($item);
}

只要在创建页面时这些元素都不存在,就可以将它们添加到哈希表中,而不是通过 DOM 进行搜索。我认为你也会受益于像mustache.js这样的 JS 模板引擎

于 2013-03-04T12:25:43.637 回答
0

您可以使用多种方式动态创建....

$("#elementid").after("<input type='checkbox'></input><span></span>;

$("#elementid").before("<input type='checkbox'></input><span></span>;

$("#elementid").append("<input type='checkbox'></input><span></span>;

$("#elementid").prepend("<input type='checkbox'></input><span></span>;

同样明智的是,您可以使用insertAfter(), insertBefore(), appendTo(), prependTo().....

于 2014-07-29T11:26:54.247 回答