简体   繁体   English

动态将输入集添加到html表单

[英]Dynamically add set of inputs to html form

I am currently creating a HTML form, I have a section in which contains 8 text inputs. 我当前正在创建HTML表单,我有一个部分包含8个文本输入。 This is the section so far: 这是到目前为止的部分:

<div class="form-title"><h3>Clinical Information</h3></div>
    <div class="form-title">Time Assesed:</div>
    <input class="form-field" type="time" name="timeassessd" /><br />
    <div class="form-title">Blood Pressure:</div>
    <input class="form-field" type="text" name="bp" /><br />
    <div class="form-title">Pulse:</div>
    <input class="form-field" type="date" name="pulse" /><br />
    <div class="form-title">Resp. Rate:</div>
    <input class="form-field" type="text" name="breathing" /><br />
    <div class="form-title">Temp:</div>
    <input class="form-field" type="text" name="temp" /><br />
    <div class="form-title">SPO2:</div>
    <input class="form-field" type="text" name="spo2" /><br />
    <div class="form-title">GCS:</div>
    <input class="form-field" type="text" name="gcs" /><br />
    <div class="form-title">AVPU:</div>
    <input class="form-field" type="text" name="avpu" /><br />

What I need is to have a button that when the user presses the button it will create another section identical to the one above adding the fields to the form. 我需要的是一个按钮,当用户按下该按钮时,它将创建与上面相同的另一部分,将字段添加到表单中。 Each form will also need a number on the end of the name. 每个表格的名称末尾也需要一个数字。 I have looked around at different forums but I cant find one with a whole section to add in just individual inputs which doesn't help me here. 我浏览过不同的论坛,但找不到完整的部分来添加单独的输入,这对我没有帮助。 Thanks. 谢谢。

You need to make a JS function to add the section. 您需要制作一个JS函数来添加该部分。 the function would look something like this: 该函数将如下所示:

function add_section() {
        new_row = "";
        new_row += "";
        new_row += '<div class="form-title"><h3>Clinical Information</h3></div>';
        new_row += '<div class="form-title">Time Assesed:</div>';
        new_row += '<input class="form-field" type="time" name="timeassessd" /><br />';
        new_row += '<div class="form-title">Blood Pressure:</div>';
        new_row += '<input class="form-field" type="text" name="bp" /><br />';
        new_row += '<div class="form-title">Pulse:</div>';
        new_row += '<input class="form-field" type="date" name="pulse" /><br />';
        new_row += '<div class="form-title">Resp. Rate:</div>';
        new_row += '<input class="form-field" type="text" name="breathing" /><br />';
        new_row += '<div class="form-title">Temp:</div>';
        new_row += '<input class="form-field" type="text" name="temp" /><br />';
        new_row += '<div class="form-title">SPO2:</div>';
        new_row += '<input class="form-field" type="text" name="spo2" /><br />';
        new_row += '<div class="form-title">GCS:</div>';
        new_row += '<input class="form-field" type="text" name="gcs" /><br />';
        new_row += '<div class="form-title">AVPU:</div>';
        new_row += '<input class="form-field" type="text" name="avpu" /><br />';

        var pos = $("selecter"); //element selecter after which you need to add the section
        $(pos).after(new_row);
}

And then on the click of the button, call this function. 然后单击按钮,调用此函数。 It will work. 它会工作。

Also the name of input fields should be an array ex: name="avpu[]" If not using array, the post method would only get the value of the last input element having same names. 输入字段的名称也应为数组ex: name="avpu[]"如果不使用数组,则post方法将仅获取具有相同名称的最后一个输入元素的值。

Could you post the whole form? 您能张贴整个表格吗?

What you can do, if you're using jQuery, is, that you clone the jQuery node of the form and than manipulate the input names, and than append the content to your form. 如果使用的是jQuery,您可以做的就是克隆表单的jQuery节点,然后操作输入名称,然后将内容附加到表单中。
Something like that: 像这样:

var num = 1;
function add_form_elements(num) {
   var clonedForm = $('#id_of_the_form').clone();
   clonedForm.find('input').each(function(id, elm) {
     elm.attr("name",elm.attr("name") + num);
   });
   $('#id_of_the_form').append(clonedForm.innerHTML);
   num++;
});

Than you need to add an EventListener to your button and bind the add_form_elements function to it. 比您需要将EventListener添加到您的按钮并将add_form_elements函数绑定到它。

Make new div, which content all you want to duplicate and is invisible. 新建一个div,其中所有要复制的内容都是不可见的。

<div class="copyable" style="display: none;">
  <div class="form-title"><h3>Clinical Information</h3></div>
  <div class="form-title">Time Assesed:</div>
  <input class="form-field" type="time" name="timeassessd" /><br />
  <div class="form-title">Blood Pressure:</div>
  <input class="form-field" type="text" name="bp" /><br />
  <div class="form-title">Pulse:</div>
  <input class="form-field" type="date" name="pulse" /><br />
  <div class="form-title">Resp. Rate:</div>
  <input class="form-field" type="text" name="breathing" /><br />
  <div class="form-title">Temp:</div>
  <input class="form-field" type="text" name="temp" /><br />
  <div class="form-title">SPO2:</div>
  <input class="form-field" type="text" name="spo2" /><br />
  <div class="form-title">GCS:</div>
  <input class="form-field" type="text" name="gcs" /><br />
  <div class="form-title">AVPU:</div>
  <input class="form-field" type="text" name="avpu" /><br />
</div>

In JS file: 在JS文件中:

function add_elm(){
  var content = $('.copyable').html(),
      $elm = $('.elm'); //element where you want to add copyable content
  $elm.append(content);
}

Note: append use for appending html inside parent node. 注意:append用于在父节点内附加html。 If you want add html exactly after other elm, just use after(content) at the end of code above. 如果要在其他Elm之后添加html,只需在上述代码末尾使用after(content)

One approach to your problem, using plain JavaScript is the following (bear in mind that this does require a change to your HTML, in that the section containing the <input> elements you wish to duplicate requires a class-name (here it's "clinicalInformation" , but adjust to your own requirements - remembering to change the selector held in the <button> elements' data-duplicates attribute): 以下是使用普通JavaScript解决问题的一种方法(请记住,这确实需要对HTML进行更改,因为包含要复制的<input>元素的部分需要一个类名(此处为"clinicalInformation" ,但要根据自己的要求进行调整-记住更改<button>元素的data-duplicates属性中保留的选择器):

// the event is passed automagically from the addEventListener()
// method:
function duplicate(event) {
  // preventing the clicked-element's default behaviour
  // (which in many cases could cause a page reload):
  event.preventDefault();

  // using Array.prototype.slice, with Function.prototype.call,
  // on the NodeList returned by document.querySelectorAll(),
  // to create an array of element nodes;
  // 'this.dataset.duplicates' retrieves the attribute-value from
  // the 'data-duplicates' attribute of the clicked element:
  var allCurrent = Array.prototype.slice.call(document.querySelectorAll(this.dataset.duplicates), 0),
    // getting the last element from the array of nodes:
    toClone = allCurrent[allCurrent.length - 1],

    // cloning that node, including its child elements:
    clone = toClone.cloneNode(true),

    // creating a RegExp (regular expression) literal,
    // to match a sequence of one, or more, numbers (\d+)
    // followed by the end of the string ($):
    reg = /\d+$/,

    // creating an 'empty'/unitialised variable for use
    // within the (later) loop:
    childElems;

  // adding the created clone to the allCurrent array:
  allCurrent.push(clone);

  // using Array.prototype.forEach() to iterate over the
  // array, the arguments (names are user-defined):
  // - first (here 'fieldset') is the current array element
  //   over which we're iterating,
  // - second (here 'index') is the index of the current
  //   array element in the array:
  allCurrent.forEach(function(fieldset, index) {

    // finding all descendant elements contained within
    // the current array-element that have a 'name' attribute,
    // using a CSS attribute-selector within
    // document.querySelectorAll():
    childElems = fieldset.querySelectorAll('[name]');

    // iterating over those descendant elements in the
    // array-like NodeList:
    Array.prototype.forEach.call(childElems, function(elem) {

      // if the regular expression matches the name (
      // RegExp.prototype.test() returning a Boolean true/false
      // based on the string matching, or not matching, the
      // regular expression) we replace that match with the index
      // (from the outer loop), or if not we simply append the
      // index to the current name property-value:
      elem.name = reg.test(elem.name) ? elem.name.replace(reg, index) : elem.name + index;
    });

    // navigating from the cloned node to its parent and, using
    // Node.insertBefore(), we insert the created clone before
    // the nextSibling of that cloned node:
    toClone.parentNode.insertBefore(clone, toClone.nextSibling);
  });

}

// getting a reference to the element that should trigger
// the duplication:
var addMore = document.getElementById('duplicate');

// adding an event-handler for the 'click' event
// (note the lack of parentheses):
addMore.addEventListener('click', duplicate)

 function duplicate(event) { event.preventDefault(); var allCurrent = Array.prototype.slice.call(document.querySelectorAll(this.dataset.duplicates), 0), toClone = allCurrent[allCurrent.length - 1], clone = toClone.cloneNode(true), reg = /\\d+$/, childElems; allCurrent.push(clone); allCurrent.forEach(function(fieldset, index) { childElems = fieldset.querySelectorAll('[name]'); Array.prototype.forEach.call(childElems, function(elem) { elem.name = reg.test(elem.name) ? elem.name.replace(reg, index) : elem.name + index; }); toClone.parentNode.insertBefore(clone, toClone.nextSibling); }); } var addMore = document.getElementById('duplicate'); addMore.addEventListener('click', duplicate) 
 label { display: block; } 
 <!-- here we're using an actual <form> element --> <form action="#" method="post"> <!-- using a fieldset to group the related fields together --> <fieldset class="clinicalInformation"> <!-- the <legend> element semantically titles that group of related fields --> <legend class="form-title"> Clinical Information </legend> <!-- the <label> semantically associates a text-label with a specific form-element; that form-element (<input />, <textarea>, <select>) can appear within the <label>, or the <label> can have a 'for' attribute equal to the 'id' of the associated element. --> <label class="form-title">Time Assesed: <input class="form-field" type="time" name="timeassessd" /> </label> <label class="form-title">Blood Pressure: <input class="form-field" type="text" name="bp" /> </label> <label class="form-title">Pulse: <input class="form-field" type="date" name="pulse" /> </label> <label class="form-title">Resp. Rate: <input class="form-field" type="text" name="breathing" /> </label> <label class="form-title">Temp: <input class="form-field" type="text" name="temp" /> </label> <label class="form-title">SPO<sub>2</sub>: <input class="form-field" type="text" name="spo2" /> </label> <label class="form-title">GCS: <input class="form-field" type="text" name="gcs" /> </label> <label class="form-title">AVPU: <input class="form-field" type="text" name="avpu" /> </label> </fieldset> <fieldset> <button id="duplicate" data-duplicates=".clinicalInformation">Add more</button> </fieldset> </form> 

Using jQuery, however, since you've used that tag for your question: 但是,由于使用了jQuery标签,因此使用了jQuery:

// binding an anonymous click event-handler, using on():
$('#duplicate').on('click', function(event) {
  // preventing the default action:
  event.preventDefault();

  // finding all elements that match the selector from
  // the clicked-element's 'data-duplicates' attribute:
  var allCurrent = $(this.dataset.duplicates),

    // finding the last of those elements:
    toClone = allCurrent.last(),

    // creating the clone, including the child elements:
    clone = toClone.clone(true),

    // the regular expression (as above):
    reg = /\d+$/;

  // adding the clone to the 'allCurrent' collection,
  // then iterating over them with each(), getting a
  // reference to the index of each element in the collection:
  allCurrent.add(clone).each(function(index) {

    // finding all descendant elements that have a name attribute,
    // updating the 'name' property of each of those found
    // elements using prop():
    $(this).find('[name]').prop('name', function(i, n) {
      // the first argument (here: 'i') is the index of the
      // current element in the collection,
      // the second (here: 'n') is the current value of the
      // current element's property.

      // exactly the same as above:
      return reg.test(n) ? n.replace(reg, index) : n + index;
    });
  });

  // inserting the clone into the document
  // after the toClone element:
  clone.insertAfter(toClone);
});

 $('#duplicate').on('click', function(event) { event.preventDefault(); var allCurrent = $(this.dataset.duplicates), toClone = allCurrent.last(), clone = toClone.clone(true), reg = /\\d+$/; allCurrent.add(clone).each(function(index) { $(this).find('[name]').prop('name', function(i, n) { return reg.test(n) ? n.replace(reg, index) : n + index; }); }); clone.insertAfter(toClone); }); 
 label { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="#" method="post"> <fieldset class="clinicalInformation"> <legend class="form-title"> Clinical Information </legend> <label class="form-title">Time Assesed: <input class="form-field" type="time" name="timeassessd" /> </label> <label class="form-title">Blood Pressure: <input class="form-field" type="text" name="bp" /> </label> <label class="form-title">Pulse: <input class="form-field" type="date" name="pulse" /> </label> <label class="form-title">Resp. Rate: <input class="form-field" type="text" name="breathing" /> </label> <label class="form-title">Temp: <input class="form-field" type="text" name="temp" /> </label> <label class="form-title">SPO<sub>2</sub>: <input class="form-field" type="text" name="spo2" /> </label> <label class="form-title">GCS: <input class="form-field" type="text" name="gcs" /> </label> <label class="form-title">AVPU: <input class="form-field" type="text" name="avpu" /> </label> </fieldset> <fieldset> <button id="duplicate" data-duplicates=".clinicalInformation">Add more</button> </fieldset> </form> 

References: 参考文献:

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

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