简体   繁体   English

如何在JQuery中获得CheckBox的ID?

[英]How to accses The ID of CheckBox in JQuery?

I want to, when I double click the card the dialog pop up. 我想,当我双击卡片弹出对话框时。 Then it is possible to create dynamic checkBoxes. 然后可以创建动态checkBoxes。 When creating the checkBoxes it is possible to edit the text, of each CheckBoxes. 创建复选框时,可以编辑每个复选框的文本。 The problem comes if I have eg. 如果我有例如,问题就来了。 created 3 checkboxes and want to edit one of them, all the others checkboxes get the same name as the one I want to edit. 创建了3个复选框并想要编辑其中一个,所有其他复选框的名称与我要编辑的名称相同。 You can see the problem in the image below: 您可以在下图中看到问题:

图片

Jquery: jQuery的:

function addCheckbox(name, status) {
        status = status || false;

        var container = $('#divboxs');
        var inputs = container.find('input');
        var id = inputs.length + 1;
        var data = {
            status: status,
            name: name
        };

        var div = $('<div />', { class: 'allcheckbox' });
        $('<input />', {
            type: 'checkbox',
            id: 'cb' + id,
            value: name
        }).prop('checked', status).on('change', function () {
            data.status = $(this).prop('checked');
        }).appendTo(div); /* set checkbox status and monitor changes */

        $('<label />', {
            'for': 'cb' + id,
            text: name
        }).appendTo(div);

        var $editCheckBox = $('<p />', {
            class: 'editCheckBox',
            text: 'Edit'
        }).appendTo(div);

        div.appendTo(container);

        container.data('checkboxes').push(data);
    }

        $('#divboxs').on('click', '.editCheckBox', function () {
        var text = $(this).parents(".allcheckbox").find("label").text();
        var input = $('<input id="attribute" value="' + text + '" />')
        $('.allcheckbox').text('').append(input);
        input.select();

        input.blur(function () {
            var text = $('#attribute').val();
            $('#attribute').parent().text(text);
            $('#attribute').remove();
        });
    });    

});

I think this is the part of the code that gives me problems: var input = $('<input id="attribute" value="' + text + '" />') 我认为这是代码中给我带来问题的部分: var input = $('<input id="attribute" value="' + text + '" />')

I think I should use the ID of CheckBox: id: 'cb' + id , instead of id="attribute" . 我想我应该使用CheckBox的ID: id: 'cb' + id ,而不是id="attribute" How to insert the id of checkBox at this place ? 如何在这个地方插入checkBox的id?

Live Demo 现场演示

Ok. 好。 So there are a few issues with your code. 因此,您的代码存在一些问题。

The first being. 第一个是。 You append the newly created input to all "allcheckbox" class elements 您将新创建的输入附加到所有“allcheckbox”类元素

$('.allcheckbox').text('').append(input);

The second issue, is in that same line you are emptying that entire DIV. 第二个问题是,在同一行中,你正在清空整个DIV。 Which will create issues once you want to update the input and label with the new value. 一旦您想要更新输入并使用新值标记,这将产生问题。

So rather hide any elements you would not want to display, once the blur event is called, remove the new input, update the values then show the elements you previously hide. 因此,隐藏您不想显示的任何元素,调用模糊事件后,删除新输入,更新值,然后显示先前隐藏的元素。

Find an updated fiddle below: http://jsfiddle.net/62QY8/122/ 在下面找到一个更新的小提琴: http//jsfiddle.net/62QY8/122/

Also, on a bit of a side note. 另外,还有一点注意事项。 "class" is a JavaScript reserved word. “class”是一个JavaScript保留字。 So rather use "classname". 所以请使用“classname”。 ie. 即。

    var $editCheckBox = $('<p />', {
        classname: 'editCheckBox',
        text: 'Edit'
    }).appendTo(div);

I am not so sure what exactly is being done here. 我不太确定这里到底做了什么。 But if the idea is to edit on checkbox at a time then please chek the following fiddle 但是,如果想要一次编辑复选框,那么请请下面的小提琴

http://jsfiddle.net/62QY8/121/ http://jsfiddle.net/62QY8/121/

    $(function () {
// Click function to add a card
var $div = $('<div />').addClass('sortable-div');

var cnt = 0,
    $currentTarget;
$('#AddCardBtn').click(function () {
    var $newDiv = $div.clone(true);
    cnt++;
    $newDiv.prop("id", "div" + cnt);

    $newDiv.data('checkboxes', []);

    $('#userAddedCard').append($newDiv);
    //      alert($('#userAddedCard').find("div.sortable-div").length);        
});

// Double click to open Modal Dialog Window
$('#userAddedCard').dblclick(function (e) {
    $currentTarget = $(e.target);   

    $('.allcheckbox').remove(); // Remove checkboxes
    $('#modalDialog').data('checkboxes', []); /* Reset dialog checkbox data */

    /* Add checkboxes from card data */
    $.each($currentTarget.data('checkboxes'), function (i, checkbox) {
        addCheckbox(checkbox.name, checkbox.status);
    });

    $('#modalDialog').dialog({
        modal: true,
        height: 600,
        width: 500,
        position: 'center'
    });
    return false;

});

$("#Getbtn").on("click", function () {

    $currentTarget.data('checkboxes', $('#modalDialog').data('checkboxes')); 
    /* Copy checkbox data to card */

    $('#modalDialog').dialog("close");
});

// Add a new checkBox
$('#btnSaveCheckBox').click(function () {
    addCheckbox($('#checkBoxName').val());
    $('#checkBoxName').val("");
});

function addCheckbox(name, status) {
    status = status || false;

    var container = $('#divboxs');
    var inputs = container.find('input');
    var id = inputs.length + 1;
    var data = {
        status: status,
        name: name
    };

    var div = $('<div />', { class: 'allcheckbox' ,id: 'div_'+ id });
    $('<input />', {
        type: 'checkbox',
        id: 'cb' + id,
        value: name
    }).prop('checked', status).on('change', function () {
        data.status = $(this).prop('checked');
    }).appendTo(div); /* set checkbox status and monitor changes */

    $('<label />', {
        'for': 'cb' + id,
        'div': 'div_' + id,
        text: name
    }).appendTo(div);

    var $editCheckBox = $('<p />', {
        class: 'editCheckBox',
        text: 'Edit'
    }).appendTo(div);

    div.appendTo(container);

    container.data('checkboxes').push(data);
}

    $('#divboxs').on('click', '.editCheckBox', function () {
    var text = $(this).parents(".allcheckbox").find("label").text();
  var id=$(this).parents(".allcheckbox").find("label").attr('div');
    var input = $('<input id="attribute" value="' + text + '" />');

    $('#'+id).html('').append(input);
    input.select();

    input.blur(function () {
        var text = $('#attribute').val();
        $('#attribute').parent().text(text);
        $('#attribute').remove();
    });
});    

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM