Jquery动态添加标签到动态创建的复选框

时间:2014-07-28 13:24:44

标签: jquery dynamic checkbox label

这是我动态创建复选框的代码(它运行良好),我现在还必须为每个添加一个标签(使用attr:id,与相应的复选框相同,类名为" Label")

$(function() {
var uniqueId = 1;
// will run on DOMDocumentLoaded
// check all paragraphs inside of element with id=fm
$('.click').each(function() {
    var $p = $(this);
    var divContent = $p.text();
    if (!divContent.match(/[£€\$¥*]/)) {
        // paragraph does not contain a currency symbol: skip
        return;
    }
    if ($p.find('input[type=radio]').length > 0) {
        // paragraph contains a radio button: skip
        return; 
    }
    // create a textbox element with appropriate attributes
    var $cbox = $('<input />').attr({
    type: 'checkbox',
    name: 'T' + (uniqueId++),
            id: 'T' + (uniqueId++),
    value: divContent,
        "class": 'toggletop'
    });
    // add spacing to the left
    $cbox.css('margin-left', '1em');
    // append the checkbox to the paragraph
    $p.before($cbox);

// edited below - I think the name, label  id, and label for is problematic. I do not know how to do it

var $boxLabel = $('<label for="+id+">NoNeed</label>').attr({
name: 'T' + (uniqueId++),
id: 'T' + (uniqueId++),
});
$p.after($boxLabel);

});});

这是html(点击按钮是在我决定添加标签之前按预期生成的):

<div id="b16" class="itempackage">
<div id="copywrapper">
<img class="Small-im" src="%%GLOBAL_SiteURL%%/i/%%GLOBAL_userid%%/p/b16.jpg"  onerror="imgError(this)" />
<div class="click">%%GLOBAL_b16%%</div></div></div>
<div id="b17" class="itempackage">
<div id="copywrapper">
<img class="Small-im" src="%%GLOBAL_SiteURL%%/i/%%GLOBAL_userid%%/p/b17.jpg"  onerror="imgError(this)" />
<div class="click">%%GLOBAL_b17%%</div></div></div>

那些包含货币符号描述和价格的var:%% GLOBAL_b16 %%

0 个答案:

没有答案