简体   繁体   English

通过单击按钮,在该表单下添加了相同的表单,但是我如何获取打印次数的值并将其存储在表中

[英]by click of the button the same form is added under that form but how can i get the value of the number of times it getting printed &store it in table

by click of the button the same form is added under that form but how can i get the value of the number of times it getting printed &store it in table.通过单击按钮,在该表单下添加了相同的表单,但是我如何获得打印次数的值并将其存储在表中。 how can i insert the data into table if the user add more experiences from model.如果用户从 model 添加更多体验,我如何将数据插入表中。 On the click of the button the value in the value field should increament too and how can i enter if the user add any number of experinces so how can i enter th data in the table.单击按钮时,值字段中的值也应该增加,如果用户添加任意数量的经验,我如何输入,那么我如何在表中输入数据。 I am inserting this data in table in row wise fashion in table`我在表格中以行方式将这些数据插入表格`

<div class="col-md-12" id="addexperience">
  <button type="button" class="btn-primary"  style="float:right;" onclick="addexperience()">Add work experience</button>
  <input type="hidden" name="1experience" id="1experience" value="<?php $i = 1; ?>">
  <div class="form-group">
   <label>If yes, fill the following table</label>
   <div class="col-md-3">
    <label>Status</label>
    <select class="form-control" name="status">
     <option value="">Select</option>
     <option value="1" <?php sel($student_experience['status'], '1'); ?>>Current</option>
     <option value="0" <?php sel($student_experience['status'], '0'); ?>>Past</option>
    </select>
   </div>
   <div class="col-md-3">
    <label>Designation</label>
    <input type="text"  class="form-control" name="designation" value="<?php echo esc_output($student_experience['designation']);?>">
   </div>
   <div class="col-md-3">
    <label>Duration</label>
    <input type="number"  class="form-control" name="duration" value="<?php echo esc_output($student_experience['duration']);?>">  
   </div>
   <div class="col-md-3">
    <label>Employer</label>
    <input type="text"  class="form-control" name="employer" value="<?php echo esc_output($student_experience['employer']);?>">
   </div>
  </div>
</div>

<script>
  function addexperience(){
    var i = $('#1experience').val();
    i++; 
    $('#addexperience').append('<div class="col-md-3"> <label>Status</label> <select class="form-control" name="status'+i+'">'+
                          '<option value="">Select</option>'+
                          '<option value="Current">Current</option>'+
                          '<option value="Past">Past</option> </select> </div>'+

                      '<div class="col-md-3">'+
                        '<label>Designation</label>'+
                        '<input type="text"  class="form-control" name="designation'+i+'">'+
                      '</div>'+

                      '<div class="col-md-3">'+
                        '<label>Duration</label>'+
                        '<input type="text"  class="form-control" name="duration'+i+'">'+  
                      '</div>'+

                      '<div class="col-md-3">'+
                        '<label>Employer</label>'+
                        '<input type="text"  class="form-control" name="employer'+i+'">'+
                      '</div>'+
                    '</div>');
                     

    $(function () {
      $('#1experience').val(i);
    });
}
</script>

` `

If you modify the HTML slightly in both the initial page display and within the javascript function so that the newly added items are grouped within a single container( div ) and the initial elements are similarly grouped and a name assigned to the immediate ancestor of the initially displayed elements ( here I chose data-id='receiver' ) then this matter of deducing the number of times the elements have been added becomes a matter of counting the child elements within the common parent - querySelectorAll being useful here though no doubt jQuery has a concise method to do this too.如果您在初始页面显示和 javascript function 中稍微修改 HTML ,以便新添加的项目在单个容器中分组( div )并且初始元素的初始元素的名称被分配给最初分组的名称显示的元素(这里我选择了data-id='receiver' )然后这个推断元素被添加次数的问题变成了计算公共父元素中的子元素的问题 - querySelectorAll在这里很有用,尽管毫无疑问 jQuery 有一个简洁的方法来做到这一点。

With that said though if you were to use the array syntax form form element names, such as designation[] or duration[] when the form is submitted you will have access to each element as an array in PHP ( or other backend language ) which will also, as a byproduct, reveal that same number of elements.话虽如此,但如果您在提交表单时使用数组语法表单元素名称,例如designation[]duration[] ,您将可以访问 PHP (或其他后端语言)中的每个元素作为数组,其中作为副产品,还会显示相同数量的元素。 Using the array syntax means you do not need to try to keep track of this number within the client使用数组语法意味着您无需尝试在客户端中跟踪此数字

 function addexperience() { let i=document.querySelectorAll('[data-id="receiver"] > div').length; $('[data-id="receiver"]').append('<div><div class="col-md-3"> <label>Status</label> <select class="form-control" name="status' + i + '">' + '<option value="">Select</option>' + '<option value="Current">Current</option>' + '<option value="Past">Past</option> </select> </div>' + '<div class="col-md-3">' + '<label>Designation</label>' + '<input type="text" class="form-control" name="designation' + i + '">' + '</div>' + '<div class="col-md-3">' + '<label>Duration</label>' + '<input type="text" class="form-control" name="duration' + i + '">' + '</div>' + '<div class="col-md-3">' + '<label>Employer</label>' + '<input type="text" class="form-control" name="employer' + i + '">' + '</div>' + '</div></div>'); alert(i) }
 [data-id='receiver']{border:1px solid red;margin:1rem;padding:1rem;} [data-id='receiver'] > div{margin:1rem;border:1px solid black;padding:0.5rem}
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="col-md-12" id="addexperience"> <button type="button" class="btn-primary" style="float:right;" onclick="addexperience()">Add work experience</button> <div data-id='receiver' class="form-group"> <div> <label>If yes, fill the following table</label> <div class="col-md-3"> <label>Status</label> <select class="form-control" name="status"> <option value="">Select</option> <option value="1" <?php sel($student_experience[ 'status'], '1'); ?>>Current</option> <option value="0" <?php sel($student_experience[ 'status'], '0'); ?>>Past</option> </select> </div> <div class="col-md-3"> <label>Designation</label> <input type="text" class="form-control" name="designation" value="<?php echo esc_output($student_experience['designation']);?>"> </div> <div class="col-md-3"> <label>Duration</label> <input type="number" class="form-control" name="duration" value="<?php echo esc_output($student_experience['duration']);?>"> </div> <div class="col-md-3"> <label>Employer</label> <input type="text" class="form-control" name="employer" value="<?php echo esc_output($student_experience['employer']);?>"> </div> </div> <!-- additional content inserted here within common parent --> </div> </div>

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

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