Adding rows dynamically with jQuery

前端 未结 5 990
天命终不由人
天命终不由人 2020-11-28 02:48

I\'m building a form where I need multiple optional inputs, what I have is basically this:

Every time a user presses the plus button a new row of form inputs

相关标签:
5条回答
  • 2020-11-28 03:01

    As an addition to answers above: you probably might need to change ids in names/ids of input elements (pls note, you should not have digits in fields name):

    <input name="someStuff.entry[2].fieldOne" id="someStuff_fdf_fieldOne_2" ..>
    

    I have done this having some global variable by default set to 0:

    var globalNewIndex = 0;
    

    and in the add function after you've cloned and resetted the values in the new row:

                    var newIndex = globalNewIndex+1;
                    var changeIds = function(i, val) {
                        return val.replace(globalNewIndex,newIndex);
                    }
                    $('#mytable tbody>tr:last input').attr('name', changeIds ).attr('id', changeIds );
                    globalNewIndex++;
    
    0 讨论(0)
  • 2020-11-28 03:04

    This will get you close, the add button has been removed out of the table so you might want to consider this...

    <script type="text/javascript">
        $(document).ready(function() {
            $("#add").click(function() {
              $('#mytable tbody>tr:last').clone(true).insertAfter('#mytable tbody>tr:last');
              return false;
            });
        });
    </script>
    

    HTML markup looks like this

      <a  id="add">+</a></td>
      <table id="mytable" width="300" border="1" cellspacing="0" cellpadding="2">
      <tbody>
        <tr>
          <td>Name</td>
        </tr>
        <tr class="person">
          <td><input type="text" name="name" id="name" /></td>
        </tr>
        </tbody>
      </table>
    

    EDIT To empty a value of a textbox after insert..

        $('#mytable tbody>tr:last').clone(true).insertAfter('#mytable tbody>tr:last');
        $('#mytable tbody>tr:last #name').val('');
        return false;
    

    EDIT2 Couldn't help myself, to reset all dropdown lists in the inserted TR you can do this

    $("#mytable tbody>tr:last").each(function() {this.reset();});           
    

    I will leave the rest to you!

    0 讨论(0)
  • 2020-11-28 03:09

    Untested. Modify to suit:

    $form = $('#my-form');
    
    $rows = $form.find('.person-input-row');
    
    $('button#add-new').click(function() {
    
        $rows.find(':first').clone().insertAfter($rows.find(':last'));
    
        $justInserted = $rows.find(':last');
        $justInserted.hide();
        $justInserted.find('input').val(''); // it may copy values from first one
        $justInserted.slideDown(500);
    
    
    });
    

    This is better than copying innerHTML because you will lose all attached events etc.

    0 讨论(0)
  • 2020-11-28 03:14

    Building on the other answers, I simplified things a bit. By cloning the last element, we get the "add new" button for free (you have to change the ID to a class because of the cloning) and also reduce DOM operations. I had to use filter() instead of find() to get only the last element.

    $('.js-addNew').on('click', function(e) {
       e.preventDefault();
       var $rows   = $('.person'),
           $last   = $rows.filter(':last'),
           $newRow = $last.clone().insertAfter($last);
    
       $last.find($('.js-addNew')).remove(); // remove old button
       $newRow.hide().find('input').val('');
       $newRow.slideDown(500);
    });
    
    0 讨论(0)
  • 2020-11-28 03:21

    I have Tried something like this and its works fine;

    enter image description here

    this is the html part :

    <table class="dd" width="100%" id="data">
    <tr>
    <td>Year</td>
    <td>:</td>
    <td><select name="year1" id="year1" >
    <option value="2012">2012</option>
    <option value="2011">2011</option>
    </select></td>
    <td>Month</td>
    <td>:</td>
    <td width="17%"><select name="month1" id="month1">
      <option value="1">January</option>
      <option value="2">February</option>
      <option value="3">March</option>
      <option value="4">April</option>
      <option value="5">May</option>
      <option value="6">June</option>
      <option value="7">July</option>
      <option value="8">August</option>
      <option value="9">September</option>
      <option value="10">October</option>
      <option value="11">November</option>
      <option value="12">December</option>
    </select></td>
    <td width="7%">Week</td>
    <td width="3%">:</td>
    <td width="17%"><select name="week1" id="week1" >
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
    </select></td>
    <td width="8%">&nbsp;</td>
    <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
    <td>Actual</td>
    <td>:</td>
    <td width="17%"><input name="actual1" id="actual1" type="text" /></td>
    <td width="7%">Max</td>
    <td width="3%">:</td>
    <td><input name="max1" id="max1" type="text" /></td>
    <td>Target</td>
    <td>:</td>
    <td><input name="target1" id="target1" type="text" /></td>
    </tr>
    

    this is Javascript part;

    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script type='text/javascript'>
    //<![CDATA[
    $(document).ready(function() {
    var currentItem = 1;
    $('#addnew').click(function(){
    currentItem++;
    $('#items').val(currentItem);
    var strToAdd = '<tr><td>Year</td><td>:</td><td><select name="year'+currentItem+'" id="year'+currentItem+'" ><option value="2012">2012</option><option value="2011">2011</option></select></td><td>Month</td><td>:</td><td width="17%"><select name="month'+currentItem+'" id="month'+currentItem+'"><option value="1">January</option><option value="2">February</option><option value="3">March</option><option value="4">April</option><option value="5">May</option><option value="6">June</option><option value="7">July</option><option value="8">August</option><option value="9">September</option><option value="10">October</option><option value="11">November</option><option value="12">December</option></select></td><td width="7%">Week</td><td width="3%">:</td><td width="17%"><select name="week'+currentItem+'" id="week'+currentItem+'" ><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select></td><td width="8%"></td><td colspan="2"></td></tr><tr><td>Actual</td><td>:</td><td width="17%"><input name="actual'+currentItem+'" id="actual'+currentItem+'" type="text" /></td><td width="7%">Max</td> <td width="3%">:</td><td><input name="max'+currentItem+'" id ="max'+currentItem+'"type="text" /></td><td>Target</td><td>:</td><td><input name="target'+currentItem+'" id="target'+currentItem+'" type="text" /></td></tr>';
      $('#data').append(strToAdd);
    
     });
     });
    
     //]]>
     </script>
    

    Finaly PHP submit part:

        for( $i = 1; $i <= $count; $i++ )
    {
        $year = $_POST['year'.$i];
        $month = $_POST['month'.$i];
        $week = $_POST['week'.$i];
        $actual = $_POST['actual'.$i];
        $max = $_POST['max'.$i];
        $target = $_POST['target'.$i];
        $extreme = $_POST['extreme'.$i];
        $que = "insert INTO table_name(id,year,month,week,actual,max,target) VALUES ('".$_POST['type']."','".$year."','".$month."','".$week."','".$actual."','".$max."','".$target."')";
        mysql_query($que);
    
    }
    

    you can find more details via Dynamic table row inserter

    0 讨论(0)
提交回复
热议问题