1

我正在尝试创建一些 javascript,每次按下按钮时都会创建一个新的文本字段,任何帮助将不胜感激,似乎 javascript 不想运行不止一次

<!DOCTYPE html>
<html>
    <head>
        <title>Q&A Admin Panel</title>
    </head>
<body>
    <h1>Q&A Admin Panel</h1>
    <form action="add_question.php" method="post">
    Question Type: <input type="text" id="questionType" />
    Question Name: <input type="text" id="questionName" />
    Question Text: <input type="text" id="questionText" />
    <select id="myList" onchange="selectType()">
    <option>Yes or No</option>
    <option>Multiple Choice</option>  
    <option>Multiple Select</option>
    <option>Open Response</option>
    </select>
    <div id='buttons'> </div>
    </form>
    <script type="text/javascript">
    function selectType()
    {
    var type=document.getElementById("myList");
    if(type == "Multiple Choice" or type == "Multiple Select"){
    // add answer = visible
        }
    }
    </script>

    <script type="text/javascript">
    var answers = 0;
    function addAnswer()
    {
    write = document.getElementById('buttons');
    write.innerHTML = write.innerHMTL + "add answer: <input type=\"text\" id=\"answer" + answers + "\" <br>";
    answers = answers + 1;
    }
    </script>
    <button onclick="addAnswer(); return false;">add answer</button>
</body>
</html>
4

3 回答 3

3
var answers = 0,
    write = document.getElementById('buttons');

function addAnswer() {
    write.innerHTML += 'Add answer: <input type="text" id="answer"' + answers + '/> <br />';
    answers++;
}​
于 2012-06-29T20:08:07.097 回答
2

我在大学项目中遇到了同样的问题。您也可以按照 David 的建议完成您的工作,但使用 innerHTML 不会向 DOM 添加元素,因此,当您刷新页面时,文本字段将消失。因此,要获取持久文本字段,您可以使用下面提到的代码:

var i = 0;

function addMore()
{
   var x = document.getElementById('buttons');
   var input1 = document.createElement("input");
   input1.setAttribute("type","text");
   input1.setAttribute("name","i" + i );
   x.appendChild( input1 );
   i++;
}  

你可以使用 firebug 来调试 javascript 的东西。谢谢。

于 2013-03-04T23:35:14.277 回答
0
function addTextField(id){
    var colors = new Array('#660000','#33ff00','#0066ff','#cc3399','#9966ff');
    var container = document.getElementById(id);
    var ulElement = document.createElement('ul');
    container.appendChild(ulElement);
    var hideLink = function(){
        var firstElement = ulElement.firstChild.getElementsByTagName('a')[0];
            firstElement.style.display = (ulElement.childNodes.length==1)?'none':'inline';
        for(var i = 0 ; i <ulElement.childNodes.length; i++)
            ulElement.childNodes[i].style.color = colors[i%5];
    }
    var addListElement = function(){
        var liElement = document.createElement('li');
        ulElement.appendChild(liElement);
        var textElement = document.createElement('input');
        textElement.setAttribute('type','text');
        liElement.appendChild(textElement);

        var deleteLink = document.createElement('a');
        deleteLink.href = "#";
        deleteLink.appendChild(document.createTextNode('delete'));
        liElement.appendChild(deleteLink);
        deleteLink.onclick = function(){
            ulElement.removeChild(liElement);
            hideLink();
        }
        hideLink();
    }
    addListElement();
    var anchorElement = document.createElement('a');
    anchorElement.href = "#";
    anchorElement.appendChild(document.createTextNode('Add more'));
    container.appendChild(anchorElement);
    anchorElement.onclick = addListElement;
    hideLink();

}

这是演示 http://jsfiddle.net/mannejkumar/cjpS2/

于 2014-01-28T15:33:12.583 回答