简体   繁体   English

单击按钮时的新文本字段

[英]New Text field when Button is clicked

I have a problem that hopefully someone can help me with.我有一个问题,希望有人可以帮助我。 I have a webpage I've been building with html and php that has text fields that you fill out and submit to create an evaluation.我有一个网页,我一直在用 html 和 php 构建,其中包含您填写并提交以创建评估的文本字段。 What I would like to do is be able to click the Add Another Criteria button to add another criteria name and description field to the page and the Add criteria and create buttons would then go below the newest set of text fields created.我想要做的是能够单击“添加另一个条件”按钮将另一个条件名称和描述字段添加到页面,然后“添加条件”和“创建”按钮将位于创建的最新文本字段集下方。 创建评估页面

All this information is going to be stored in a database using PHP controller files so I am not sure if using Javascript would be the best way to achieve my goal.所有这些信息都将使用 PHP 控制器文件存储在数据库中,因此我不确定使用 Javascript 是否是实现我的目标的最佳方式。 I apologize for my lack of php knowledge I am very new to this any help would be much appreciated Thank you!我为我缺乏 php 知识而道歉我对此很陌生,任何帮助将不胜感激谢谢!

Use jQuery to add an onClick Listener and add the fields:使用 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>');
});

This would be easier if you're using jQuery, but without, it's still possible.如果您使用 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);

On the PHP side, using the brackets will make the value come through as an array:在 PHP 方面,使用方括号将使值作为数组出现:

 for ($x = 0; $x < $_POST['criteria']; $x++) {
    // do something with each $_POST['criteria'][$x] and $_POST['description'][$x]
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM