javascript - jQuery - 为什么当我单击一个图标时会附加 2 个元素

标签 javascript jquery jquery-ui

我在这里创建了一个 JSfiddle:

基本上我有一个允许用户输入额外部分的表单...但是当我添加了超过 2 个单元然后继续单击“加号”(+) 图标时,我得到了超过 1 个元素创建于该部分...它可能是一些基本内容,但任何信息都会有所帮助。

最佳答案

将您的点击功能移出点击功能

//add unit input box and increment click counter by one. 
addUnit.click(function () {

    unitCounter += 1;

    unitElementCount = jQuery(".unit-element").length;

    if (unitCounter <= 4) {
        error.hide();
        container.append('<table id="unit-' + unitCounter + '-div" class="create-course-table-element unit-element"><tr><td><label class="unit-label">Unit ' + unitCounter + '</label></td><td><input class="create-course-input-element unit-input" id="unit-id-' + unitCounter + '" name="unit-' + unitCounter + '" /><div id="delete-unit-' + unitCounter + '" class="ui-icon ui-icon-circle-close del-unit" title="Delete unit"></div></td></tr><tr><td align="center">Sections</td><td><div id="add-section-icon-' + unitCounter + '" class="ui-icon ui-icon-plus add-section-icon"></div></td></tr></table><div id="section-id-' + unitCounter + '-div" class="this-section"></div>');
    } else if (unitElementCount == 4) {
        unitCounter = 5;
        error.html("");
        error.fadeIn(1500);
        error.append("<p class='error-message'>Note: You are only able to add 4 units to a given course. Each unit allows you to add 10 separate sections of content; therefore you may add a total of 40 different sections to a given course. If the material requires more units, you should consider dividing the course into 2 parts.</p>");
    }

});
    //This part has been slightly modified and moved out of the addUnit.click() function
    var counterSecTwo = 0;
    var counterSecThree = 0;
    var counterSecFour = 0;

    jQuery(document).on("click", "#add-section-icon-2",function () {

        counterSecTwo += 1;
        var container = jQuery("#section-id-2-div");
    container.append("<p>test "+counterSecTwo+"</p>");

    });

    jQuery(document).on("click", "#add-section-icon-3",function () {

        counterSecThree += 1;
        var container = jQuery("#section-id-3-div");
    container.append("<p>test "+counterSecThree+"</p>");

    });

    jQuery(document).on("click", "#add-section-icon-4",function () {

        counterSecFour += 1;
        var container = jQuery("#section-id-4-div");
    container.append("<p>test "+counterSecFour+"</p>");

    });
});

这里我将点击处理程序绑定(bind)到 Document,因为元素尚不存在:您也可以在创建实际元素时添加事件监听器。

修改后的 fiddle :http://jsfiddle.net/vewP7/

关于javascript - jQuery - 为什么当我单击一个图标时会附加 2 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23813955/

相关文章:

javascript - 触发 javascript 函数的其他选项

javascript - 如何根据我对某个对象内容的了解,从数组中查找并删除该对象?

javascript - 填充 SVG 元素,处理 JSON 数据

javascript - JSON 数据未返回 - JQuery AJAX

php - 通过ajax访问时服务器端函数抛出php错误

javascript - knockout .js : Binding to a function on the click-event using the Repository-Pattern

javascript - 电话号码正则表达式验证

jquery - jqGrid 和 Seo - 他们可以成为 friend 吗?

jquery - 页面 : Close all other tabs when another is opened 上有多个 jQuery UI 选项卡

jquery - 如何将链接列表更改为 jQuery UI 主题按钮列表?