javascript - 如何使卡片独有的对话框

标签 javascript jquery css html

当我将卡片添加到收件箱列表时,我可以双击它并弹出一个对话框窗口。在对话框窗口中,我可以从日历中选择一个日期,将一个字符串值放入输入字段并创建一个动态复选框。然后,当我按下按钮(获取值)时,值会出现在卡上。这很好用。当我将另一张卡片添加到收件箱并双击它以打开对话框窗口时,问题就来了,显示了与前一个对话框窗口相同的值。我不想要它。 我希望每次添加一张新卡片并双击它时,对话框窗口都没有值。

我认为解决方案可能是使卡片独一无二,但我不确定。

请帮助我,并提供一些代码。

DEMO

HTML:

    <!--Inbox list and button to add a card-->
    <div id="inboxList" class="cellContainer">
        <p style="display: inline">Inbox</p> 
        <!--Button to add a Card-->
        <input type="button" id="AddCardBtn" value="+ Add a Card..."/> <hr class="fancy-line"/> <br/>

        <!--Card div-->
        <div id="userAddedCard"> <br/>
            <div>

            </div>
        </div>
    </div>


</div>
<!--Modal Dialog-->
<div id="modalDialog">

    <form>
        <label>Title</label>
        <input type="text" id="customTextBox" value="some value"/>
        <p>Date: <input type="text" id="datepicker" value="some date"/></p> 
        <input type="button" id="Getbtn" value="Get value"/> <hr/><br/>

        <label>Add checkBox</label>
        <br />
        <div id="progressbar"></div>
        <br />
        <input type="text" id="checkBoxName" />
        <input type="button" id="btnSaveCheckBox" value="_Ok" />
        <br />

    </form>

</div>
<!--Reference to Jquery-->

J查询:

$(function () {
    // Click function to add a card
    var $div = $('<div />').addClass('sortable-div'); 
    $('<label>Title</label><br/>').appendTo($div);              
    $('<input/>', { "type": "text","class":"ctb"}).appendTo($div);
    $('<input/>', { "type": "text","class":"date"}).appendTo($div);
    var cnt =0,$currentTarget;
    $('#AddCardBtn').click(function () {
      var $newDiv = $div.clone(true);
      cnt++;  
      $newDiv.prop("id","div"+cnt);  
      $('#userAddedCard').append($newDiv);

    });

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

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

    });
    $("#datepicker").datepicker({showWeek:true, firstDay:1});

    $("#Getbtn").on("click",function() {
      var val = $("#customTextBox").val();
      $currentTarget.find(".ctb").val(val);
      $currentTarget.find(".date").val($("#datepicker").val() );
      $('#modalDialog').dialog("close");
    });

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

    function addCheckbox(name) {
        var container = $('#modalDialog');
        var inputs = container.find('input');
        var id = inputs.length + 1;

        $('<input />', { type: 'checkbox', id: 'cb' + id, value: name }).appendTo(container);
        $('<label />', { 'for': 'cb' + id, text: name }).appendTo(container);
        $('<br/>').appendTo(container);


    }    
});

最佳答案

for your code add the lines that i added
$(function () {
// Click function to add a card
var $div = $('<div />').addClass('sortable-div'); 
$('<label>Title</label><br/>').appendTo($div);              
$('<input/>', { "type": "text","class":"ctb"}).appendTo($div);
$('<input/>', { "type": "text","class":"date"}).appendTo($div);
var cnt =0,$currentTarget;
$('#AddCardBtn').click(function () {
  var $newDiv = $div.clone(true);
  cnt++;  
  $newDiv.prop("id","div"+cnt);  
  $('#userAddedCard').append($newDiv);

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

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

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

});
$("#datepicker").datepicker({showWeek:true, firstDay:1});

$("#Getbtn").on("click",function() {
  var val = $("#customTextBox").val();
  $currentTarget.find(".ctb").val(val);
  $currentTarget.find(".date").val($("#datepicker").val() );
    document.getElementById('customTextBox').value="";//add this line
    document.getElementById('datepicker').value="";//add this line
    $('.allcheckbox').remove();//add this line
  $('#modalDialog').dialog("close");
});

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

function addCheckbox(name) {
    var container = $('#modalDialog');
    var inputs = container.find('input');
    var id = inputs.length + 1;

    $('<input />', { type: 'checkbox', class: 'allcheckbox', id: 'cb' + id, value: name }).appendTo(container);//add the attribute class
    $('<label />', { 'for': 'cb' + id, class: 'allcheckbox', text: name }).appendTo(container);//add the attribute class
    $('<br/>').appendTo(container);


}    

});

关于javascript - 如何使卡片独有的对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22378874/

相关文章:

javascript - API消费者和数据转换

javascript - 在 jQuery 中停止项目多次滑动

javascript - 如果目标文件不存在,则在 Node.js 中异步重命名文件

jquery - 在 jQuery 中绑定(bind)元素及其子元素

javascript - 使用多个类更改字体

javascript - 如何使div静态化,这样如果屏幕有两个大它就不会跳起来

css - Material-UI 下拉列表在对话框中溢出

javascript - 我可以隐藏滚动条但保持溢出吗?

javascript - AngularJS:过滤器数组

jquery - 无法获取下拉选择的值