Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击按钮添加HTML表单_Javascript_Html_Laravel - Fatal编程技术网

Javascript 单击按钮添加HTML表单

Javascript 单击按钮添加HTML表单,javascript,html,laravel,Javascript,Html,Laravel,我正在为一个学校项目制作一个“测验网站”,但我有点被一个部分卡住了 我们需要在测验中添加问题,所以我想制作一个按钮“添加问题”,它添加1个(或更多)html表单 我几乎没有JavaScript经验,只知道基本知识 我的Laravel.blade文件: {{ Form::open(array('url'=>'quizzes/edit', 'class' => 'createquiz')) }} <p>Question 1</p>{{ Form::text

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

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

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

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

<input type="button" onclick="appendQuestion()" value="Add new question">
Jquery append()也适用于您

如果您对编码很认真,请访问w3c或mdn。微软也有一个很好的指南,用于在某处设计事物的样式。你可以在这里找到令人惊奇的卫斯理
请随时问我任何问题。

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

<input type="button" onclick="appendQuestion()" value="Add new question">
Jquery append()也适用于您

如果您对编码很认真,请访问w3c或mdn。微软也有一个很好的指南,用于在某处设计事物的样式。你可以在这里找到令人惊奇的卫斯理
请随时问我任何问题。

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

<input type="button" onclick="appendQuestion()" value="Add new question">
Jquery append()也适用于您

如果您对编码很认真,请访问w3c或mdn。微软也有一个很好的指南,用于在某处设计事物的样式。你可以在这里找到令人惊奇的卫斯理
请随时问我任何问题。

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

<input type="button" onclick="appendQuestion()" value="Add new question">
Jquery append()也适用于您

如果您对编码很认真,请访问w3c或mdn。微软也有一个很好的指南,用于在某处设计事物的样式。你可以在这里找到令人惊奇的卫斯理
请随时问我任何问题。

好的,根据我从您的评论中收集的信息,您希望做以下事情:

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

var限制=10;//最大问题
变量计数=4;//已经有4个问题了
函数addQuestion()
{
//获取测验表单元素
var quick=document.getElementById('quick');
//做错误检查很好,确保我们得到了一些东西
如果(测验)
{
如果(计数<限制)
{
//创建一个新的元素
var newP=document.createElement('p');
newP.innerHTML='Question'+(count+1);
//创建新的文本框
var newInput=document.createElement('input');
newInput.type='text';
newInput.name='questions[]';
//进行错误检查的良好实践
if(newInput&&newP)
{
//将新元素添加到表单中
儿童智力测验(newP);
appendChild(newInput);
//增加计数
计数++;
}
}
其他的
{
警报(“已达到问题限制”);
}
}
}
问题1

问题2

问题3

问题4


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

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

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

var限制=10;//最大问题
变量计数=4;//已经有4个问题了
函数addQuestion()
{
//获取测验表单元素
var quick=document.getElementById('quick');
//做错误检查很好,确保我们得到了一些东西
如果(测验)
{
如果(计数<限制)
{
//创建一个新的元素
var newP=document.createElement('p');
newP.innerHTML='Question'+(count+1);
//创建新的文本框
var newInput=document.createElement('input');
newInput.type='text';
newInput.name='questions[]';
//进行错误检查的良好实践
if(newInput&&newP)
{
//将新元素添加到表单中
儿童智力测验(newP);
appendChild(newInput);
//增加计数
计数++;
}
}
其他的
{
警报(“已达到问题限制”);
}
}
}
问题1

问题2

问题3

问题4


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

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

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

var限制=10;//最大问题
变量计数=4;//已经有4个问题了
函数addQuestion()
{
//获取测验表单元素
var quick=document.getElementById('quick');
//做错误检查很好,确保我们得到了一些东西
如果(测验)
{
如果(计数<限制)
{
//创建一个新的元素
var newP=document.createElement('p');
newP.innerHTML='Question'+(count+1);
//创建新的文本框
var newInput=document.createElement('input');
newInput.type='text';
newInput.name='questions[]';
//进行错误检查的良好实践
if(newInput&&newP)
{
//将新元素添加到表单中
儿童智力测验(newP);
appendChild(newInput);
//增加计数
计数++;
}
}
其他的
{
警报(“已达到问题限制”);
}
}
}
问题1