如何将附加元素的总数限制为div?

时间:2017-01-11 19:16:50

标签: jquery

我有两个按钮,分别为div添加两个不同的图像。我已经能够限制他们的个人追加四。所以我可以添加每个图像中的四个(总共八个)。但是,我想将图像总数限制为六个。例如,如果我添加四个" image1",我只能添加2个" image2"。



Add-in Express Registrator Log File: 01/11/2017 15:36:47

Installation directory: C:\WINDOWS\Installer\
Registrator version: 8.4.4395.0
Operating System: Microsoft Windows 10 Professional (build 14393), 64-bit
Process Owner: System
Command Line: "C:\WINDOWS\Installer\MSI5FB0.tmp" /install="C:\Program Files\Blah\Blah.dll" /log=%RoamingAppDataFolder%/Blah
Run 'As Administrator': Yes
Process Elevated: Yes
Integrity Level: System
UAC (User Account Control): On
--------------------------------------------------------------
15:36:47 0448 Starting the add-in registration process.
15:36:47 0448 Loading mscoree.dll
15:36:47 0448 Success.
15:36:47 0448 .NET Framework installation directory: 
15:36:47 0448 The latest version of .NET Framework: 'v4.0.30319'
15:36:47 0448 Loading CLR: v4.0.30319.
15:36:47 0448 Calling CLRCreateInstance method.
15:36:47 0448 Success.
15:36:47 0448 Calling GetRuntime method.
15:36:47 0448 Success.
15:36:47 0448 Checking if the hosting API of .NET Framework v4.0 beta is installed.
15:36:47 0448 The hosting API is up to date.
15:36:47 0448 Calling GetInterface method for the CorRuntimeHost interface.
15:36:47 0448 Success.
15:36:47 0448 Starting CLR...
15:36:47 0448 Success.
15:36:47 0448 Getting the CLR version.
15:36:47 0448 The CLR v4.0.30319 has been initialized successfully.
15:36:47 0448 Creating a new domain setup.
15:36:47 0448 Success.
15:36:47 0448 The 'shadow copy' is disabled.
15:36:47 0448 Creating a new application domain.
15:36:47 0448 Success.
15:36:47 0448 Getting the base directory for the domain.
15:36:47 0448 Success. The directory is 'C:\Program Files\Blah\'.
15:36:47 0448 Searching for the Add-in Express core library.
15:36:47 0448 Success. The 'AddinExpress.MSO.2005.dll' file is found.
15:36:47 0448 Creating an instance of the 'AddinExpress.Deployment.ADXRegistrator' class.
15:36:47 0448 Assembly identity is 'AddinExpress.MSO.2005'.
15:36:47 0448 Success.
15:36:47 0448 Unwrapping the instance of the 'AddinExpress.Deployment.ADXRegistrator' class.
15:36:47 0448 Success.
15:36:47 0448 Calling the managed registration procedure (DISPID = 1610743823).
15:36:48 0448 Registration success.
15:36:48 0448 The add-in registration process is completed with HRESULT = 0.




2 个答案:

答案 0 :(得分:0)

你很亲密。首先 - 不要使用2个文档就绪函数,并使变量可用于两种单击方法(如果你在同一个就绪函数体内就是这种情况) - 这应该这样做。

$(document).ready(function () {
    var maxAppend = 6: // your total value
    var currAppended = 0; // currently added

    $("#addcement").click(function () {   
        var $imageElement = $("<div class='sample_holder' ><div class='cement'><input type='image' src='Images/sample_thumbs/61_cement_smallglass.jpg' width='5%' name='closeStory' class='closebtn' width='22px'></div></div>");
        if (currAppended >= maxAppend) return; // if added >= max return
        $("#samplebox").append($imageElement);
        currAppended++; // increase the variable
        $("i").append(addinput);
    });

    $("#addbiscuit").click(function () {
        var $imageElement = $("<div class='sample_holder' ><div class='biscuit'><input type='image' src='Images/sample_thumbs/102_biscuit.jpg' width='5%' name='closeStory' class='closebtn' width='22px'></div></div>");
        if (currAppended >= maxAppend) return; // same here
        $("#samplebox").append($imageElement);
        currAppended++; // increase the variable
        $("i").append(addinput);    
    });

    $("#samplebox").on('click', '.close_bisq_btn', function(){
        $(this).closest('.biscuit').remove();
        currAppended++; // decrease the variable
    });

    $("#samplebox").on('click', '.closebtn', function(){
        $(this).closest('.cement').remove();
        currAppended--; // decrease the variable
    })
});

答案 1 :(得分:0)

&#13;
&#13;
{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "noEmitOnError": true,
    "allowJs": false,
    "sourceMap": true,
    "strictNullChecks": true
  },
  "exclude": ["dist", "scripts"]
}
&#13;
&#13;
&#13;