简体   繁体   English

如何创建一个动态下拉选择元素,其选项计数根据某个 div 的子元素数量而变化?

[英]How to create a dynamic dropdown selection element whose option count changes according to the number to child a certain div has?

I have a div of certain id (say #pageContent ) whose number of child element is going to change.我有一个特定 id(比如#pageContent )的 div,其子元素的数量将发生变化。 I have a selection dropdown (say pageSelection )whose option count should match the number of children of #pageContent .我有一个选择下拉菜单(比如pageSelection ),其期权数应匹配的儿童人数#pageContent If the #pageContent child count is 10, the number of options in pageSelection should also be 10...如果#pageContent子计数为 10,则pageSelection的选项数也应为 10...

The last three options of the dropdown is to remain unchanged but the values must change accordingly.下拉列表的最后三个选项保持不变,但值必须相应更改。 Initially the last three options have the value 2, 3 & 4.最初最后三个选项的值为 2、3 和 4。
If two more child elements are added to #pageContent , then the values of the last three options should now become 4, 5 & 6.如果另外两个子元素添加到#pageContent ,那么最后三个选项的值现在应该变为 4、5 和 6。
If 3 more child elements were added to #pageContent , then the values of the last three options should should have become 5, 6 & 7 and so on...如果另外 3 个子元素被添加到#pageContent ,那么最后三个选项的值应该变成 5、6 和 7 等等......

Here's my Code:这是我的代码:

 function prependOptions() { var count = document.getElementById("pageContent").childElementCount; for (i = 0; i < count - 3; i++) { var index = i + 1; document.getElementById("pageSelection").prepend("<option value=\\'" + index + "\\'>" + index + "</option>"); } } $(function() { $("#pageSelection").change(function() { var selectedText = $(this).find("option:selected").text(); var selectedValue = $(this).val(); alert("Selected Text: " + selectedText + " Value: " + selectedValue); }); }); function addElement(parentId, elementTag, html) { var p = document.getElementById(parentId); var newElement = document.createElement(elementTag); newElement.innerHTML = html; p.prepend(newElement); }
 <input type="button" onclick="addElement('pageContent', 'div','Added Fruit');" value="Add an Item" /> <div id="pageContent"> <div class="pageContentItem">Content01</div> <div class="pageContentItem">Content02</div> <div class="pageContentItem">Apple</div> <div class="pageContentItem">Mango</div> <div class="pageContentItem">Orange</div> </div> <select name="contentSelector" id="pageSelection"> <option value="8">Apple</option> <option value="9">Mango</option> <option value="10">Orange</option> </select>

codepen is available for easy trails. codepen可用于简单的路径。

Here below is Snippet which would help you ,下面是片段,它会帮助你,

Modified the addElement Method to achieve the same.修改了 addElement 方法以实现相同的效果。

function addElement(parentId, elementTag, html) {
  var p = document.getElementById(parentId);
  var newElement = document.createElement(elementTag);
  newElement.innerHTML = html;
  p.prepend(newElement);
  var elementValues = [];
  $("#pageContent").children().each(function () { 
    elementValues.push($(this).html()); 
  });
  $('#pageSelection').find('option').remove().end();
  $.each(elementValues, function( index, value ) {
    $('#pageSelection').append($('<option>', { value : index }).text(value));
  });
}

Here below is the updated code penURL:以下是更新后的代码 penURL:

https://codepen.io/redhatvicky/pen/jRPEXq?editors=1010 https://codepen.io/redhatvicky/pen/jRPEXq?editors=1010

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

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