【问题标题】:How to append a row with user entered text value dynamically added rows?如何使用用户输入的文本值追加一行动态添加的行?
【发布时间】:2019-11-26 15:10:50
【问题描述】:

我有一个输入字段和一个引导模式框,我想将值附加到动态添加的行中,

例如,如果我单击添加行并单击输入字段打开弹出窗口并输入一些值,例如“Joe”,每当我单击添加行时,该值 (Joe) 会附加到每一行。

如果我想编辑一行数据只想更改的行,例如现在我只需添加一行并输入一些文本(此处为一些文本),然后我添加 4 行相同的文本(此处为一些文本) 将附加到 4 行,然后如果我编辑第 3 行输入文本,我只想更改第 3 行文本。不适用于所有行..

我希望我的问题得到理解。

这里是例子..

这里是小提琴的例子..

Fiddle Here..

示例代码在这里..

$(document).ready(function() {
  $("#ok_button").on("click", function() {
    $("#acc_narrat0").val($("#cash_text:first").val());
  });
  $("#add_Row").on("click", function() {
    var counter = 0;
    var idVal = $('#tab_logic tr:last').find('td:first').html();
    var matches = idVal.match(/\d+/g);
    if (matches != null) {
      counter = Number(matches) + counter + 1;
    }
    var newRow = $("<tr>");
    var cols = "";

    cols += '<td><input value="' + $("#cash_text:first").val() + '" type="text" class="form-control required price" name="narr" placeholder="Enter your text here" id="acc_narrat' + counter + '" data-toggle="modal" data-target="#narratModal" onchange="unname(this.id, this.value)"/></td>'

    cols += '<td><button type="button" class="adRow ibtnDel" style="width:70%;">x</button></a></td>';

    newRow.append(cols);


    var defVal = $("select[name=acctname]").find(":selected").val();
    if (defVal) {
      $("select[name=accountName]").find(`option[value=${defVal}]`).hide();
    }
    $("table.order-list").append(newRow);
    setValCashVal('accountName'.concat(counter));
    bindScript();
    counter++;

  });
  $("table.order-list").on("click", ".ibtnDel", function(_event) {
    $(this).closest("tr").remove();
    if ($("#tab_logic tbody tr").length == 1)
      $("#cash_text:first").val('');
    evaluateTotal();

  });
});

/* Apend value to all row */
$("#ok_button").on('click', function() {
  let val = $("#cash_text").val();
  $("#pay_narrat, #acc_narrat").val(val);
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>


<input type="button" class="add_Row adRow" id="add_Row" value="Add Row">
<table class="table table-bordered table-hover order-list" id="tab_logic" style="width:60% !important">
  <thead>
    <tr style="background-color: #680779; color: #fff;">
      <th class="text-center">
        Description*
      </th>
    </tr>
  </thead>
  <tbody>
    <tr id="fst_row">
      <td>

      </td>
    </tr>
  </tbody>
</table>

<!-- narrtion modal -->

<div class="modal fade" id="narratModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close cash-dismiss" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title modal_head" id="myModalLabel">Description</h4>
      </div>
      <div class="modal-body">
        <textarea id="cash_text" class="cash_text" maxlength="200" placeholder="Enter your text here.."></textarea>
      </div>
      <div class="modal-footer narr_footer">
        <button id="ok_button" type="button" class="btn btn-primary cashmodal_btn" data-dismiss="modal">OK</button>
      </div>
    </div>
  </div>
</div>

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    $(document).ready(function() {
      $("#ok_button").on("click", function() {
      
        $(".act").val($("#cash_text:first").val());
      });
      $("#add_Row").on("click", function() {
     
        var counter = 0;
        var idVal = $('#tab_logic tr:last').find('td:first').html();
        var matches = idVal.match(/\d+/g);
        if (matches != null) {
          counter = Number(matches) + counter + 1;
        }
        var newRow = $("<tr>");
        var cols = "";
        if($('.price').length > 0){
        	var first_ele = document.querySelector('.price').value;
        }else{
        	var first_ele ='';
        }
    		
        cols += '<td><input value="' + first_ele + '" type="text" class="form-control required price" name="narr" placeholder="Enter your text here" id="acc_narrat' + counter + '" data-toggle="modal" data-target="#narratModal" onchange="unname(this.id, this.value)"/></td>'
    
        cols += '<td><button type="button" class="adRow ibtnDel" style="width:70%;">x</button></a></td>';
    
        newRow.append(cols);
    
    
        var defVal = $("select[name=acctname]").find(":selected").val();
        if (defVal) {
          $("select[name=accountName]").find(`option[value=${defVal}]`).hide();
        }
        $("table.order-list").append(newRow);
        //setValCashVal('accountName'.concat(counter));
       // bindScript();
        counter++;
    
      });
      $("table.order-list").on("click", ".ibtnDel", function(_event) {
        $(this).closest("tr").remove();
        if ($("#tab_logic tbody tr").length == 1)
          $("#cash_text:first").val('');
        evaluateTotal();
    
      });
    });
    
    /* Apend value to all row */
    $("#ok_button").on('click', function() {
      //alert("in");
      let val = $(".act").val();
      $("#pay_narrat, #acc_narrat").val(val);
    });
    $(document).on('click','.price',function(){
    	$('.price').removeClass('act');
    	$(this).addClass('act');
      $("#cash_text").val($(this).val())
    })
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    
    
    <input type="button" class="add_Row adRow" id="add_Row" value="Add Row">
    <table class="table table-bordered table-hover order-list" id="tab_logic" style="width:60% !important">
      <thead>
        <tr style="background-color: #680779; color: #fff;">
          <th class="text-center">
            Description*
          </th>
        </tr>
      </thead>
      <tbody>
        <tr id="fst_row">
          <td>
    
          </td>
        </tr>
      </tbody>
    </table>
    
    <!-- narrtion modal -->
    
    <div class="modal fade" id="narratModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close cash-dismiss" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title modal_head" id="myModalLabel">Description</h4>
          </div>
          <div class="modal-body">
            <textarea id="cash_text" class="cash_text" maxlength="200" placeholder="Enter your text here.."></textarea>
          </div>
          <div class="modal-footer narr_footer">
            <button id="ok_button" type="button" class="btn btn-primary cashmodal_btn" data-dismiss="modal">OK</button>
          </div>
        </div>
      </div>
    </div>

    【讨论】:

      【解决方案2】:

      主要问题在这一行:$("#acc_narrat0").val($("#cash_text:first").val()) 它总是指第一个元素。要解决此问题,您可以设置一个变量,该变量在聚焦后引用输入。

      此外,您可以使用hide.bs.modal 事件在模态文本区域关闭后对其进行清理。您可以shown.bs.modal 将 textarea 集中在显示上。

      $(document).ready(function() {
        let inputElment = null;
        $("table").on("focus", "input[type='text']", function() {
          inputElment = jQuery(this);
        });
      
        $('#narratModal').on('hide.bs.modal', function() {
          $('#narratModal textarea').val("");
        });
      
        $('#narratModal').on('shown.bs.modal', function() {
          $('#narratModal textarea').focus();
        });
      
      
        $("#ok_button").on("click", function() {
          inputElment.val($("#cash_text:first").val());
        });
      
        $("#add_Row").on("click", function() {
          var counter = 0;
          var idVal = $('#tab_logic tr:last').find('td:first').html();
          var matches = idVal.match(/\d+/g);
          if (matches != null) {
            counter = Number(matches) + counter + 1;
          }
          var newRow = $("<tr>");
          var cols = "";
      
          cols += '<td><input value="' + $("#cash_text:first").val() + '" type="text" class="form-control required price" name="narr" placeholder="Enter your text here" id="acc_narrat' + counter + '" data-toggle="modal" data-target="#narratModal" onchange="unname(this.id, this.value)"/></td>'
      
          cols += '<td><button type="button" class="adRow ibtnDel" style="width:70%;">x</button></a></td>';
      
          newRow.append(cols);
      
      
          var defVal = $("select[name=acctname]").find(":selected").val();
          if (defVal) {
            $("select[name=accountName]").find(`option[value=${defVal}]`).hide();
          }
          $("table.order-list").append(newRow);
          setValCashVal('accountName'.concat(counter));
          bindScript();
          counter++;
      
        });
        $("table.order-list").on("click", ".ibtnDel", function(_event) {
          $(this).closest("tr").remove();
          if ($("#tab_logic tbody tr").length == 1)
            $("#cash_text:first").val('');
          evaluateTotal();
      
        });
      });
      
      /* Apend value to all row */
      $("#ok_button").on('click', function() {
        let val = $("#cash_text").val();
        $("#pay_narrat, #acc_narrat").val(val);
      });
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
      <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
      
      
      <input type="button" class="add_Row adRow" id="add_Row" value="Add Row">
      <table class="table table-bordered table-hover order-list" id="tab_logic" style="width:60% !important">
        <thead>
          <tr style="background-color: #680779; color: #fff;">
            <th class="text-center">
              Description*
            </th>
          </tr>
        </thead>
        <tbody>
          <tr id="fst_row">
            <td>
      
            </td>
          </tr>
        </tbody>
      </table>
      
      <!-- narrtion modal -->
      
      <div class="modal fade" id="narratModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close cash-dismiss" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
              <h4 class="modal-title modal_head" id="myModalLabel">Description</h4>
            </div>
            <div class="modal-body">
              <textarea id="cash_text" class="cash_text" maxlength="200" placeholder="Enter your text here.."></textarea>
            </div>
            <div class="modal-footer narr_footer">
              <button id="ok_button" type="button" class="btn btn-primary cashmodal_btn" data-dismiss="modal">OK</button>
            </div>
          </div>
        </div>
      </div>

      【讨论】:

      • 我只是在第一行输入文本,第一行文本追加动态添加的行,每当我想编辑任何行文本只想更改的行文本时..请参考我的示例图片..
      • 它正在工作。当您尝试编辑任何行时,它将仅编辑该行中的文本。你还有什么想做的吗?
      • @Kalimah - 你是对的,但我的问题和要求是我只需单击添加行按钮将添加一行,然后我输入一些文本,然后再次单击添加行,现在相同的文本想要出现.. 每当我单击添加行,旧文本只想附加..如果我尝试编辑任何行,它将仅编辑该行中的文本。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-24
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多