最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

html - Nesting a drop down inside a section in a dynamic javascript form - Stack Overflow

matteradmin6PV0评论

I'm trying to use a drop down next to a simple number input within a section on a dynamic web page form. I am able to create a new section with a button, and I can use normal text or number fields within that section. However when I try and create a drop down within a section I cannot get it to work.

I can get the dropdown to work fine when its independent, just not within the dynamically created section.

document.getElementById('addVegetablesButton').addEventListener('click', function() {
  var newSection = document.createElement('fieldset');
  var legend = document.createElement('legend');
  legend.textContent = 'Vegetable';

  var newField1 = document.createElement('input');
  newField1.type = 'number';
  newField1.name = 'sectionField1';
  newField1.placeholder = 'Quantity';
    
  var newField2 = document.createElement('label');
  newField2.textContent = 'Vegetable type:';

  var newSelect = document.createElement('select');
  newSelect.name = 'dynamicSelect';

  var option1 = document.createElement('option');
  option1.value = 'option1';
  option1.textContent = 'Carrot';

  var option2 = document.createElement('option');
  option2.value = 'option2';
  option2.textContent = 'Brussel Sprout';

  newSelect.appendChild(option1);
  newSelect.appendChild(option2);
    
  newSection.appendChild(legend);
  newSection.appendChild(newField1);
  newSection.appendChild(newField2);
  document.getElementById('dynamicForm').appendChild(newSection);
});

With the above the first element (quantity) works fine, the label for the newField2 shows, but no drop down. I tried various configurations where the button woldn't even generate the section, the above is as close as I have come to it work.

Post a comment

comment list (0)

  1. No comments so far