单击按钮添加HTML表单
Adding a HTML form on button click
我正在为一个学校项目制作一个"智力竞赛网站",但我有点拘泥于其中一部分。
我们需要在测验中添加问题,所以我想制作一个"添加问题"按钮,添加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/请随时问我任何问题。
相关文章:
- 使用 AngularJS Http Post 将表单 html 中的数据发布到服务器
- 如何使用js框架验证Laravel 4表单(html)
- 在表单html上编程php和警告javascript
- 联系表单-Html和Javascript只包含在Html(tumblr)中
- javascript表单(html网页)名称字段的验证
- Javascript多维数组表单html列表
- 单选按钮上的无提示验证被忽略,而它只适用于相同表单html中的文本字段
- 从表单html中检索数据
- 刷新页面后是否可以保留表单(html)内容数据
- 部分表单HTML未加载
- 控制器和输入表单html使用var Javascript
- 获取表单html及其值
- 如何使表单(html、jsp)中的Textfield只接受dd/mm/yyyy格式,而无需单击提交按钮
- 提交隐藏的表单HTML
- 信用卡表单html值长度
- PHP表单HTML按钮
- 简单的方式来改变输入字段的视觉顺序动态(不改变表单HTML)
- 表单HTML没有更新
- 制作一个不序列化javascript的一对多表单html
- 搜索parse.com数据表单html输入