简体   繁体   English

使用JavaScript限制表单中的子代数

[英]Limit the number of children in a form using Javascript

I am trying to limit the number of additional form input fields that a user can add dynamically to a file upload form to just 3. The form is loaded with one static input field and through javascript can add additional fields with an add button or remove additional form input fields with a remove button. 我试图将用户可以动态添加到文件上传表单的附加表单输入字段的数量限制为3个。该表单加载有一个静态输入字段,并且通过javascript可以使用添加按钮添加其他字段或删除其他带有删除按钮的表单输入字段。 Below is the html in it's static form. 以下是静态形式的html。

<fieldset>
        <legend>Upload your images</legend>
          <ol id="add_images">
            <li>
              <input type="file" class="input" name="files[]" />
            </li>
          </ol>
         <input type="button" name="addFile"  id="addFile" value="Add Another Image" onclick="window.addFile(this);"/>
</fieldset>

With javascript I would like to create a function where the number of child 我想用javascript创建一个函数,其中子数

  • elements are counted and if the number is equal to three then the "Add Another Image" button becomes disabled. 元素计数,如果数量等于3,则“添加其他图像”按钮将被禁用。 In addition, if there are three 另外,如果有三个
  • elements in the form the user - with the remove button - removes a child 用户形式的元素-使用“删除”按钮-删除子级
  • then the "Add Another Image" button becomes enabled again. 然后“添加其他图像”按钮将再次启用。

    I think I'm may be missing some crucial lines of code. 我想我可能缺少一些关键的代码行。 The below javascript code only allows me to add one additional input field before the Add Another Image button becomes disabled. 下面的javascript代码仅允许我在“添加其他图片”按钮被禁用之前添加一个其他输入字段。 Removing this field with the remove file button removes the field but the Add Another Image button is still disabled. 使用“删除文件”按钮删除该字段将删除该字段,但是“添加其他图像”按钮仍处于禁用状态。 Below is where I'm currently at with the javascript. 以下是我目前使用javascript的位置。

     function addFile(addFileButton) { var form = document.getElementById('add_images'); var li = form.appendChild(document.createElement("li")); //add additional input fields should the user want to upload additional images. var f = li.appendChild(document.createElement("input")); f.className="input"; f.type="file"; f.name="files[]"; //add a remove field button should the user want to remove a file var rb = li.appendChild(document.createElement("input")); rb.type="button"; rb.value="Remove File"; rb.onclick = function () { form.removeChild(this.parentNode); } //create the option to dispable the addFileButton if the child nodes total "3" var nodelist; var count; nodelist = form.childNodes; count = nodelist.length; for(i = 0; i < count; i++) { if (nodelist[i] ==3) { document.getElementById("addFile").disabled = 'true'; } else { //if there are less than three keep the button enabled document.getElementById("addFile").disabled = 'false'; } } 

    } }

  • Oh, OK, I've tested out the code now and see a couple of problems: 哦,好的,我现在已经测试了代码,看到了两个问题:

    1. You're counting the number of child elements but this includes the text elements so there's actually one for the <li> and one for the text within it. 您正在计算子元素的数量,但这包括文本元素,因此实际上<li>一个,而其中的文本有一个。
    2. You've enclosed the true/false setting for the disabled property in quotes but it doesn't work and always set's it to false. 您已将禁用属性的true / false设置括在引号中,但它不起作用,始终将其设置为false。
    3. The remove button doesn't re-enable the add button. 删除按钮不会重新启用添加按钮。

    I found this to work: 我发现这可行:

        function addFile(addFileButton) {
            var form = document.getElementById('add_images');
            var li = form.appendChild(document.createElement("li"));
    
            //add additional input fields should the user want to upload additional images.
            var f = li.appendChild(document.createElement("input"));
            f.className="input";
            f.type="file";
            f.name="files[]";
    
            //add a remove field button should the user want to remove a file
            var rb = li.appendChild(document.createElement("input"));
            rb.type="button";
            rb.value="Remove File";
            rb.onclick = function () {
                form.removeChild(this.parentNode);
                toggleButton();
            }
            toggleButton();
        }
    
        function toggleButton() {
            var form = document.getElementById('add_images');
            //create the option to dispable the addFileButton if the child nodes total "3"
            var nodelist;
            var count;
            nodelist = form.childNodes;
            count = 0;
    
    
            for(i = 0; i < nodelist.length; i++) {
                if(nodelist[i].nodeType == 1) {
                    count++;
                }
            }
    
            if (count >= 3) {
                document.getElementById("addFile").disabled = true;
            }
            else { //if there are less than three keep the button enabled
                document.getElementById("addFile").disabled = false;
            }
        }
    

    I would suggest a slightly different approach. 我建议一种稍微不同的方法。 Create all three file input fields statically and provide a clear button. 静态创建所有三个文件输入字段,并提供一个清除按钮。 If the user chooses to leave it empty they can. 如果用户选择将其保留为空,则可以。 If that is not elegant use your "Remove" to simply hide the field (CSS style display: none;). 如果这不太好用,请使用“删除”简单地隐藏字段(CSS样式显示:无;)。

    The last part of that function is a bit strange. 该功能的最后一部分有些奇怪。 Technically, when adding fields, you should only be disabling the button (ie you could never enable the button by adding fields). 从技术上讲,添加字段时,您仅应禁用按钮(即,您永远不能通过添加字段来启用按钮)。 I would suggest removing the for loop and going with: 我建议删除for循环并继续:

    var count = form.getElementsByTagName("li").length;
    if(count == 3)
        document.getElementById("addFile").disabled = true;
    

    The reason the add field button is still disabled when you remove an item is because you don't re-enable the add field button when you click remove. 删除项目时仍禁用添加字段按钮的原因是,单击删除后不会重新启用添加字段按钮。 Try this for the remove button click handler: 尝试使用此操作作为“删除”按钮单击处理程序:

    rb.onclick = function () {
        form.removeChild(this.parentNode);
        document.getElementById("addFile").disabled = false;
    }
    

    I'm not sure why you're using the for loop? 我不确定为什么要使用for循环? Shouldn't it be like this: 不应该是这样的:

    var nodelist = form.childNodes;
    
    if (nodelist.length >= 3) {
        document.getElementById("addFile").disabled = 'true';
    }    
    else { //if there are less than three keep the button enabled
        document.getElementById("addFile").disabled = 'false';  
    }
    

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM