javascript - 使用 jquery 动态添加时 Selectric 不工作

标签 javascript jquery html css jquery-selectric

我使用以下代码克隆表单元素并附加到单击按钮时的目标。

   $('#addChild').on('click', function () {
                var num     = $('.clonedInput').length, 
                newNum  = new Number(num + 1),      
                newElem = $('#entry' + num).clone().attr('id', 'entry' + newNum).fadeIn('slow'); 

             if (newNum == 4) { 
                    alert("Sorry, You can add upto 3 childrens only");
                    return false; 
             } 

            newElem.find('.heading-reference').attr('id', 'ID' + newNum + '_reference').attr('name', 'ID' + newNum + '_reference').html('Child #' + newNum);

            // Title - select
            newElem.find('.fnameLabel').attr('for', 'ID' + newNum + '_title');
            newElem.find('.fName').attr('id', 'ID' + newNum + '_fName').attr('name', 'ID' + newNum + '_fName').val('');

            // First name - text
            newElem.find('.surLabel').attr('for', 'ID' + newNum + '_sName');
            newElem.find('.sName').attr('id', 'ID' + newNum + '_sName').attr('name', 'ID' + newNum + '_sName').val('');

            // Last name - text
            newElem.find('.genderLabel').attr('for', 'ID' + newNum + '_gender');
            newElem.find('.genderSelect').attr('id', 'ID' + newNum + '_gender').attr('name', 'ID' + newNum + '_gender');


            // Color - checkbox
            newElem.find('.dobLab').attr('for', 'ID' + newNum + '_dobLab');
            newElem.find('.dob').attr('id', 'ID' + newNum + '_dob').attr('name', 'ID' + newNum + '_dob');

            $('#entry' + num).after(newElem);
            $('#ID' + newNum + '_title').focus();


           $('#btnDel').attr('disabled', false);

     }); 

但问题是,在克隆之后,selectric 选择框对克隆的元素不起作用。我不知道我错过了哪里。任何帮助将不胜感激。

请检查 fiddle 。 http://jsfiddle.net/vandanasrivastava/ug1ders7/

最佳答案

这是一种间接方法,您可以从克隆对象中删除selectric 项,然后在追加到div 之前将selectric 选项重置为默认值,然后在追加的选择框中初始化selectric。

  var clone =  $('.clonedInput').clone();//Clone the input
  clone.find(".clonedInput").val(''); // Clear the selected value

  //Trigger the action here 
  var default_html = clone.find(".clonedInput").parent('div').html();//will take the html content of your select box
  //clonedInputParentDiv is the name of your clonedInput parents div
  clone.find('.clonedInputParentDiv').children('.selectric-wrapper').remove();//Removing all selectric variables from clone
  clone.find('.clonedInputParentDiv').append(title); // Append to clone as normal selectbox


  clone.find(".clonedInput").selectric();// Initialize the selectric in new appended selectbox
  clone.insertAfter('.clonedInput:last');

关于javascript - 使用 jquery 动态添加时 Selectric 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33582291/

相关文章:

javascript - 使用javascript根据第二次出现的分隔符拆分字符串

javascript - 下划线模板在 IE 中不起作用

javascript - 如何创建具有边框样式的动态响应式四列?

html - Google Lighthouse 的 Tap Target Spacing 功能有多可靠?

javascript - 一个一个地运行 jQuery 函数来执行 CSS 转换

html - 什么是加载外部资源的 HTML 元素列表? (HTML 电子邮件)

javascript - PHP 消息未写入日志文件 - tutsplus.com 教程

javascript - 如何防止页面和选项卡的 document.title 发生更改?

javascript - 字符串比较 "="!= "+",结果不正确

javascript - 延迟打开和关闭 Colorbox