Jquery - 动态添加和删除字段 - 如何在不重复代码的情况下执行此操作?

时间:2014-04-29 14:52:17

标签: javascript jquery plugins scope

我正在实现“JQuery添加/删除输入字段”解决方案: 我需要使用一些JQuery插件来使一切工作。例如,我使用Datepicker,SelectPicker和Autosize .. 因此,对于已存在的标记(没有ADD功能),此代码可以正常工作:

$(document).ready(function() {
    $('.autosize').autosize();      
    $('input, textarea').placeholder();
    $('.datetimepickaa').datetimepicker({
        pickTime: false
    });
    $('.selectpicker').selectpicker();

    // Remove the specific row
    $("button.removee").click(function(){
        $(this).closest(".conteiner").remove();
    });
});

问题在于,当我尝试添加ADD功能时,jquery插件不适用于新元素,因此我必须重复添加代码中的调用才能使其工作:

$(document).ready(function() {

    $('.autosize').autosize();      
    $('input, textarea').placeholder();
    $('.datetimepickaa').datetimepicker({
        pickTime: false
    });
    $('.selectpicker').selectpicker();

    // Remove the specific row
    $("button.removee").click(function(){
        $(this).closest(".conteiner").remove();
    });

    // ADD FUNCTIONALITY
    $("#add").click(function() {

        var row = '\
               <div class="form-group conteiner">\
                   <div class="row">\
                       <div class="col-md-2">\
                           <label for="date">Date:</label>\
                           <div class="input-group date datetimepickaa"  id="datetimepickerloop" data-date-format="YYYY/MM/DD">\
                               <input type="text" class="form-control datetimepickaa" placeholder="Enter the date..." data-date-format="YYYY/MM/DD" />\
                               <span class="input-group-addon">\
                                   <span class="glyphicon glyphicon-calendar"></span>\
                               </span>\
                           </div>\
                       </div>\
                       <div class="col-md-9">\
                           <label for="notes">Notes:</label>\
                           <textarea class="form-control autosize" id="" name=""></textarea>\
                       </div>\
                       <div style="" class="col-md-1">\
                           <button type="button" class="removee btn btn-primary btn-md pull-right" style="margin-top:25px">\
                               <span class="glyphicon glyphicon-remove"></span> Delete\
                           </button>\
                       </div>\
                   </div>\
               </div>';

        $("#wrapper").append(row);

        // REPETITION OF THE CODE ABOVE!!!!!!   //////////////////////////////
        $('.autosize').autosize();      
        $('input, textarea').placeholder();
        $('.datetimepickaa').datetimepicker({
            pickTime: false
        });
        $('.selectpicker').selectpicker();

        // Remove the specific row
        $("button.removee").click(function(){
            $(this).closest(".conteiner").remove();
        }); 

    });

});

对于如何在不重复任何代码的情况下以最佳方式进行操作,您有什么线索吗?

提前感谢!!

1 个答案:

答案 0 :(得分:1)

将它们提取到函数中并调用该函数?

function initializeThings()
{
$('.autosize').autosize();      
    $('input, textarea').placeholder();
    $('.datetimepickaa').datetimepicker({
        pickTime: false
    });
    $('.selectpicker').selectpicker();
 // Remove the specific row
    $("button.removee").click(function(){
        $(this).closest(".conteiner").remove();
    });
}

$(document).ready(function() {

initializeThings();



// ADD FUNCTIONALITY
$("#add").click(function() {

    var row = '\
           <div class="form-group conteiner">\
               <div class="row">\
                   <div class="col-md-2">\
                       <label for="date">Date:</label>\
                       <div class="input-group date datetimepickaa"  id="datetimepickerloop" data-date-format="YYYY/MM/DD">\
                           <input type="text" class="form-control datetimepickaa" placeholder="Enter the date..." data-date-format="YYYY/MM/DD" />\
                           <span class="input-group-addon">\
                               <span class="glyphicon glyphicon-calendar"></span>\
                           </span>\
                       </div>\
                   </div>\
                   <div class="col-md-9">\
                       <label for="notes">Notes:</label>\
                       <textarea class="form-control autosize" id="" name=""></textarea>\
                   </div>\
                   <div style="" class="col-md-1">\
                       <button type="button" class="removee btn btn-primary btn-md pull-right" style="margin-top:25px">\
                           <span class="glyphicon glyphicon-remove"></span> Delete\
                       </button>\
                   </div>\
               </div>\
           </div>';

    $("#wrapper").append(row);

    initializeThings();

});