下拉列表脚本

时间:2013-12-01 20:31:06

标签: javascript jquery html

如何为此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

<select name="text">
                <option value="t1">t1</option>
                <option value="t2">t2</option>
                <option value="t3">t3</option>
              </select>
              <input type="button" value=" + " onClick="addNew();">

编辑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 '<h4>Your message has been sent!</h4>';
    } else { 
        echo '<h4>Something went wrong, go back and try again!</h4>'; 
    } 
} else if ($_POST['submit'] && $human != '4') {
    echo '<h4>You answered the anti-spam question incorrectly!</h4>';
}
} else {
    echo '<h4>You need to fill in all required fields!!</h4>';
}
}
 ?>

干杯

1 个答案:

答案 0 :(得分:1)

从前一个选择框中获取值。在function addNew() {

之后添加以下代码
function addNew() {

    var countAll = document.getElementsByTagName("select").length - 1;

    var lastSelectBox = document.getElementsByTagName("select")[countAll];

    var items = lastSelectBox.innerHTML;

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

在{:1}}之后添加新的html:

items

您可以看到工作jsfiddle