[英]What am I doing wrong? I'm trying to clone some fields and use some functions
I am building a form where a select menu is cloned and appended. 我正在构建一个克隆和追加选择菜单的表单。 In each clone there is a jQuery drop-down selector that I have created a function called unitClone that you can see towards the end of the JavaScript code.
在每个克隆中都有一个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);
}
}
For some reason the code is not working properly, the first item does not allow me to click and select and the third item allows me to select but the same value is used for all the drop-downs. 由于某些原因,代码无法正常工作,第一项不允许我单击并选择,第三项允许我选择,但相同的值用于所有下拉列表。
Any help would be greatly appreciated. 任何帮助将不胜感激。
Here is my code: 这是我的代码:
http://jsfiddle.net/tlaverdure/nKrvh/ http://jsfiddle.net/tlaverdure/nKrvh/
I change the on method in your js code to live and everythings work fine 我将你的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 is the edited code. 这是编辑过的代码。
Good luck 祝好运
Found a fix. 找到了解决办法。
.live
was not working out. .live
没有运作。
obj.dd.toggle(function() {
selectArea = $(this).attr('id');
$('#'+ selectArea).addClass('active');
return false;
},
function(){
selectArea = $(this).attr('id');
$('#'+ selectArea).removeClass('active');
return true;
});
http://jsfiddle.net/tlaverdure/nKrvh/3/ http://jsfiddle.net/tlaverdure/nKrvh/3/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.