下拉列表脚本

如何向此javascript添加值数组,以便在下拉列表中列出它们? 我已经走到这一步了,我真的被卡住了,我认为它必须是javascript的一小部分。

任何帮助都会很棒。

JAVASCRIPT var counter = 0; function addNew() { // Get the main Div in which all the other divs will be added var mainContainer = document.getElementById('mainContainer'); // Create a new div for holding text and button input elements var newDiv = document.createElement('div'); // Create a new text input var newText = document.createElement('select'); newText.type = "select"; //for testing newText.value = counter++; // Create buttons for creating and removing inputs var newAddButton = document.createElement('input'); newAddButton.type = "button"; newAddButton.value = " + "; var newDelButton = document.createElement('input'); newDelButton.type = "button"; newDelButton.value = " - "; // Append new text input to the newDiv newDiv.appendChild(newText); // Append new button inputs to the newDiv newDiv.appendChild(newAddButton); newDiv.appendChild(newDelButton); // Append newDiv input to the mainContainer div mainContainer.appendChild(newDiv); // Add a handler to button for deleting the newDiv from the mainContainer newAddButton.onclick = addNew; newDelButton.onclick = function() { mainContainer.removeChild(newDiv); }; } 

HTML

  t1 t2 t3   

编辑PHP

  <?php $name = $_POST['name']; $email = $_POST['email']; $number = $_POST['number']; $Occasion = $_POST['Occasion']; $Venues = $_POST['Venues']; $date = $_POST['date']; $guests = $_POST['guests']; $custom = $_POST['custom']; $from = $email; $to = 'ash.manterfield@btinternet.com'; $subject = 'New Menu Order'; $human = $_POST['human']; $body = "From: $name\n Contact Number: $number\n E-Mail: $email\n Occasion: $Occasion\n Venues Looked At: $Venues\n Event Date: $date\n Number of Guests: $guests\n Custom Menu:\n $custom"; if ($_POST['submit']) { if ($name != '' && $email != '') { if ($human == '4') { if (mail ($to, $subject, $body, $from)) { echo '

Your message has been sent!

'; } else { echo '

Something went wrong, go back and try again!

'; } } else if ($_POST['submit'] && $human != '4') { echo '

You answered the anti-spam question incorrectly!

'; } } else { echo '

You need to fill in all required fields!!

'; } } ?>

干杯

从前一个选择框中获取值。 在function addNew() {之后添加以下代码

 function addNew() { var countAll = document.getElementsByTagName("select").length - 1; var lastSelectBox = document.getElementsByTagName("select")[countAll]; var items = lastSelectBox.innerHTML; 

在这里,我将获得文档中创建的最后一个选择框。

然后添加新的html items

 var newText = document.createElement('select'); newText.type = "select"; //Attribute Name use for form elements newText.setAttribute('name', 'text['+counter+']'); //Attribute id newText.setAttribute('id', 'text_' + counter); newText.innerHTML = items; 

你可以看到工作的jsfiddle