在按钮单击上添加HTML表单

时间:2015-03-26 10:38:35

标签: javascript html laravel

我正在为一个学校项目制作一个“测验网站”,但我很讨厌一个部分。

我们需要在测验中添加问题,所以我想创建一个“添加问题”按钮,其中添加了1个(或更多)html表单。

我的JavaScript经验很少,只知道基础知识。

My Laravel .blade文件:

{{ Form::open(array('url'=>'quizzes/edit', 'class' => 'createquiz')) }}
    <p>Question 1</p>{{ Form::text('', null, array('placeholder' => 'Question 1', 'size' => '40', 'id' => 'questions')) }}
    <p>Question 2</p>{{ Form::text('', null, array('placeholder' => 'Question 2', 'size' => '40', 'id' => 'questions')) }}
    <p>Question 3</p>{{ Form::text('', null, array('placeholder' => 'Question 3', 'size' => '40', 'id' => 'questions')) }}
    <p>Question 4</p>{{ Form::text('', null, array('placeholder' => 'Question 4', 'size' => '40', 'id' => 'questions')) }}
<br>
<br>
{{ Form::button('Add Question', array('onclick' => 'addQuestion()', 'id' => 'questionadd')) }}
{{ Form::submit('Edit Quiz') }}
{{ Form::close() }}

我的JavaScript:

function addQuestion() {
var node = document.createElement('FORM');
var counter = 1;
var limit = 3;

if (counter == limit)  {
    alert("You have reached the limit of questions");
}

else {
node.appendChild(FORM);
document.getElementById("questions").appendChild(node);
}

}

因此,点击“添加问题”按钮,我想在其他人之后添加一个问题

2 个答案:

答案 0 :(得分:2)

好的,从我的评论中收集到的内容,您希望做到这样的事情:

<script>
var limit = 10; // Max questions
var count = 4; // There are 4 questions already

function addQuestion()
{
    // Get the quiz form element
    var quiz = document.getElementById('quiz');

    // Good to do error checking, make sure we managed to get something
    if (quiz)
    {
        if (count < limit)
        {
            // Create a new <p> element
            var newP = document.createElement('p');
            newP.innerHTML = 'Question ' + (count + 1);

            // Create the new text box
            var newInput = document.createElement('input');
            newInput.type = 'text';
            newInput.name = 'questions[]';

            // Good practice to do error checking
            if (newInput && newP)   
            {
                // Add the new elements to the form
                quiz.appendChild(newP);
                quiz.appendChild(newInput);
                // Increment the count
                count++;
            }

        }
        else   
        {
            alert('Question limit reached');
        }
    }
}
</script>

<form id="quiz" action="" method="POST">

    <input type="button" value="Add question" onclick="javascript: addQuestion();"/>

    <p>Question 1</p>
    <input type="text" name="questions[]"/>
    <p>Question 2</p>
    <input type="text" name="questions[]"/>
    <p>Question 3</p>
    <input type="text" name="questions[]"/>
    <p>Question 4</p>
    <input type="text" name="questions[]"/>
    <p></p>


</form>

记下评论并仔细阅读代码,了解发生了什么。希望这会有所帮助。

答案 1 :(得分:1)

您可以尝试将innerHTML与+ =运算符一起使用。它允许您附加到您要附加的内容的末尾。以下是所有代码:

<input type="button" onclick="appendQuestion()" value="Add new question">

这是该功能:

function appendQuestion()
{
Document.getElementById('questionDivContainer').innerHTML = "code for the new question";
}

Jquery append()也适合你。

如果您认真对待编码,请访问w3c或mdn。微软还有一个很棒的指南,可以在某个地方设计样式。 www.dribbble.com is an excellent resource for styling and inspiration你可以在这里找到令人惊叹的wesites http://www.awwwards.com/websites/clean/ 随时问我任何问题。