简体   繁体   English

我究竟做错了什么? 我正在尝试克隆一些字段并使用一些函数

[英]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.

相关问题 我试图一键挂起 2 个功能,然后出现错误,我做错了什么? - I'm trying to hang 2 functions on one click, and getting an error, what am I doing wrong? 试图理解 JS 函数 - 我做错了什么? - Trying to understand JS functions - what am I doing wrong? 当我尝试更新我的产品时,我做错了什么? - What am I doing wrong, when I'm trying to update my products? 我究竟做错了什么? 我正在尝试通过jQuery单击来放大和缩小图像 - What am I doing wrong? I'm trying to make an image bigger and smaller on a click with jQuery 为什么我得到 ':' expected”?我正在尝试定义一些函数来运行一些单元测试以进行基准测试 - Why am I getting ':' expected"? I'm trying to define a few functions to run some unit tests on for a benchmark 难道我做错了什么? 我正在尝试测试是否填写了所有字段并且 API 响应是否无效 - Am I doing something wrong? I’m trying to test if all fields are filled in and an API response isn’t invalid 比较两个输入字段-我在做什么错? - Comparing two input fields - What am I doing wrong? 尝试回叫功能时我做错了 - What I'm doing wrong when trying call back function 我做错了什么? - What i am doing wrong? 当单击按钮时,我试图将输入的内容保存到本地存储中。 我究竟做错了什么? - I'm trying to save the content of an input into local storage when a button is clicked. What am I doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM