单击按钮添加HTML表单

Adding a HTML form on button click

本文关键字:表单 HTML 添加 按钮 单击      更新时间:2023-09-26

我正在为一个学校项目制作一个"智力竞赛网站",但我有点拘泥于其中一部分。

我们需要在测验中添加问题,所以我想制作一个"添加问题"按钮,添加1个(或多个)html表单。

我几乎没有JavaScript的经验,只知道一些基础知识。

我的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);
}
}

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

好吧,根据我从你的评论中收集到的信息,你希望做这样的事情:

<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>

记下注释,仔细阅读代码,看看发生了什么。希望这能有所帮助。

您可以尝试使用带有+=运算符的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是一个很好的造型和灵感资源,你可以在这里找到令人惊叹的wesiteshttp://www.awwwards.com/websites/clean/请随时问我任何问题。