【问题标题】:Adding a HTML form on button click在按钮单击时添加 HTML 表单
【发布时间】:2015-05-30 08:16:48
【问题描述】:

我正在为一个学校项目制作一个“测验网站”,但我有点卡住了一个部分。

我们需要在测验中添加问题,所以我想制作一个“添加问题”按钮,它可以添加 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);
}

}

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

【问题讨论】:

  • 您是否尝试过编写 Javascript?您是使用纯 Javascript 还是愿意使用 jQuery 等库?
  • 是的,我现在添加了。不,只是 JavaScript。我们不允许使用 jQuery
  • 好的,您是想在当前表单中添加一个问题,还是添加另一个带有 X 个问题的表单?我假设第一个。
  • Screenshot 是这个样子,点击“添加问题”按钮后,我需要添加另一个“问题”表单。

标签: javascript html laravel


【解决方案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你可以在这里找到很棒的网站http://www.awwwards.com/websites/clean/ 有任何问题都可以问我。

【讨论】:

    【解决方案2】:

    好吧,根据我从您的 cmets 那里收集到的信息,您希望执行以下操作:

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

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

    【讨论】:

    • 如果问题是多项选择题,添加选项怎么样?选项如何与问题相关联
    猜你喜欢
    • 1970-01-01
    • 2020-12-16
    • 2019-06-24
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    相关资源
    最近更新 更多