单击按钮时的新文本字段
New Text field when Button is clicked
我有一个问题,希望有人能帮助我。我有一个使用 html 和 php 构建的网页,其中包含您填写并提交以创建评估的文本字段。我想要做的是能够单击“添加另一个条件”按钮向页面添加另一个条件名称和描述字段,然后“添加条件和创建”按钮将位于最新创建的文本字段集下方。
所有这些信息都将使用 PHP 控制器文件存储在数据库中,因此我不确定使用 Javascript 是否是实现我的目标的最佳方式。对于我缺乏 php 知识,我深表歉意,我对此很陌生,如有任何帮助,我们将不胜感激谢谢!
使用jQuery添加onClick监听器并添加字段:
$("#button").on("click", function() {
$("#containertoappend").append('<input type="text" id="anothercriteria" placeholder="Another criteria">');
$("#containertoappend").append('<textarea id="anotherdescription" placeholder="Another Description"></textarea>');
});
如果您使用 jQuery,这会更容易,但如果没有,它仍然是可能的。
var newCriteria = document.createElement("input");
newCriteria.setAttribute('placeholder', 'Criteria Name');
newCriteria.name = 'criteria[]';
var newDesc = document.createElement("textarea");
newDesc.setAttribute('placeholder', 'Description');
newDesc.name = 'description[]';
document.getElementById("theForm").appendChild(newCriteria);
document.getElementById("theForm").appendChild(newDesc);
在 PHP 一侧,使用括号将使值作为数组出现:
for ($x = 0; $x < $_POST['criteria']; $x++) {
// do something with each $_POST['criteria'][$x] and $_POST['description'][$x]
}
我有一个问题,希望有人能帮助我。我有一个使用 html 和 php 构建的网页,其中包含您填写并提交以创建评估的文本字段。我想要做的是能够单击“添加另一个条件”按钮向页面添加另一个条件名称和描述字段,然后“添加条件和创建”按钮将位于最新创建的文本字段集下方。
所有这些信息都将使用 PHP 控制器文件存储在数据库中,因此我不确定使用 Javascript 是否是实现我的目标的最佳方式。对于我缺乏 php 知识,我深表歉意,我对此很陌生,如有任何帮助,我们将不胜感激谢谢!
使用jQuery添加onClick监听器并添加字段:
$("#button").on("click", function() {
$("#containertoappend").append('<input type="text" id="anothercriteria" placeholder="Another criteria">');
$("#containertoappend").append('<textarea id="anotherdescription" placeholder="Another Description"></textarea>');
});
如果您使用 jQuery,这会更容易,但如果没有,它仍然是可能的。
var newCriteria = document.createElement("input");
newCriteria.setAttribute('placeholder', 'Criteria Name');
newCriteria.name = 'criteria[]';
var newDesc = document.createElement("textarea");
newDesc.setAttribute('placeholder', 'Description');
newDesc.name = 'description[]';
document.getElementById("theForm").appendChild(newCriteria);
document.getElementById("theForm").appendChild(newDesc);
在 PHP 一侧,使用括号将使值作为数组出现:
for ($x = 0; $x < $_POST['criteria']; $x++) {
// do something with each $_POST['criteria'][$x] and $_POST['description'][$x]
}