使用 jquery 动态添加时 Selectric 不工作
Selectric not working when added dynamically using jquery
我使用以下代码克隆表单元素并附加到单击按钮时的目标。
$('#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 select 之后,框对克隆的项目不起作用。我不知道我错过了哪里。任何帮助将不胜感激。
您在 .clone()
之前初始化了 selectric,因此整个元素(已被 selectric 插件修改)被克隆.
您应该在初始化插件之前克隆该元素。
// clone it before .selectric():
var newEl = $('#entry1').clone();
// initialize plugin:
$(".basic").selectric();
$('#addChild').on('click', function () {
var num = $('.clonedInput').length,
newNum = new Number(num + 1),
// use cloned object without the data appended by selectric:
newElem = newEl.clone().attr('id', 'entry' + newNum).fadeIn('slow');
// ...
// Then initialize the plugin for the new ".basic" elements:
$(newElem).appendTo('.allCatsContainer').find(".basic").selectric();
});
另一件事是所有克隆的 <select>
元素都具有相同的 ids': (#gender
, #day
, #month
, #year
)
也修改这些 id 或如果不需要则不要使用它们。
这是一种间接方法,您可以从克隆对象中删除 selectric 项,然后在附加到 div 之前将 select 选项重置为默认值,然后初始化 selectric 在附加 select 框中。
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');
我使用以下代码克隆表单元素并附加到单击按钮时的目标。
$('#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 select 之后,框对克隆的项目不起作用。我不知道我错过了哪里。任何帮助将不胜感激。
您在 .clone()
之前初始化了 selectric,因此整个元素(已被 selectric 插件修改)被克隆.
您应该在初始化插件之前克隆该元素。
// clone it before .selectric():
var newEl = $('#entry1').clone();
// initialize plugin:
$(".basic").selectric();
$('#addChild').on('click', function () {
var num = $('.clonedInput').length,
newNum = new Number(num + 1),
// use cloned object without the data appended by selectric:
newElem = newEl.clone().attr('id', 'entry' + newNum).fadeIn('slow');
// ...
// Then initialize the plugin for the new ".basic" elements:
$(newElem).appendTo('.allCatsContainer').find(".basic").selectric();
});
另一件事是所有克隆的 <select>
元素都具有相同的 ids': (#gender
, #day
, #month
, #year
)
也修改这些 id 或如果不需要则不要使用它们。
这是一种间接方法,您可以从克隆对象中删除 selectric 项,然后在附加到 div 之前将 select 选项重置为默认值,然后初始化 selectric 在附加 select 框中。
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');