我正在构建一个表单,其中克隆并附加了选择菜单。在每个克隆中,都有一个 jQuery 下拉选择器,我创建了一个名为 unitClone 的函数,您可以在 JavaScript 代码末尾看到它。
unitClone = function(input) {
var id = $('#' + input + ' .unit_select').attr('id');
var id = ('#' + id);
//alert(id);
dd = new Select($(id));
var selected = $(id).closest('td').find('.ingredient_unit').val();
if (selected != null) {
var selected = $(id).find(".dropdown li a[data-id=" + selected + "]").html();
$(id).find('span').text(selected);
}
}
由于某种原因,代码无法正常工作,第一个项目不允许我单击并选择,第三个项目允许我选择,但所有下拉菜单都使用相同的值。
任何帮助将不胜感激。
这是我的代码:
最佳答案
我将 js 代码中的 on 方法更改为 live,一切正常
Select.prototype = {
initEvents: function() {
var obj = this;
obj.dd.live('click', function(event) {
$(this).toggleClass('active');
return false;
});
obj.opts.live('click', function() {
var opt = $(this);
obj.val = opt.text();
obj.index = opt.index();
var num = $(this).closest('td').find('.num');
var numVal = $(this).closest('td').find('.num').val();
if (numVal > 1) {
obj.val = obj.val + 's'
}
obj.placeholder.text(obj.val);
obj.opt_id = opt.find('a').data('id');
$('.recipe_difficulty').val(obj.opt_id);
});
},
getValue: function() {
return this.val;
},
getIndex: function() {
return this.index;
}
}
Here是编辑后的代码。
祝你好运
关于javascript - 我究竟做错了什么?我正在尝试克隆一些字段并使用一些函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14018659/