我创建了动态表,我想在文本框中显示选定的下拉列表值,我尝试了使用javascript和jQuery的多种方法,但在我的动态表中无法正常工作。

我只想在文本框中传递值

你能帮我吗?


  注意:仅传递值表下拉列表。


这是我详细的小提琴

Fiddle


  用于创建动态表的jQuery代码




$(document).ready(function() {
  $("#cashacc_sel").html($('#cashacc').html());
  var i = 1;
  $("#add_row").click(function() {
    var oSelectedValue = $('#cashacc').val();
    $('#addr' + i).html("<td><input name='cashpayment[" + i + "].name' type='text' placeholder='Enter code' id='cashacc_code' class='form-control input-md'/></td><td><select class='form-control input-md' name='cashpayment[" + i + "].name' id='dynamic_sel'><option>Choose an items</option></select></td>");
    // {/* <td>" + (i + 1) + "</td> */}
    $("#cashacc_sel").find("select").append().appendTo($("#dynamic_sel"));
    $('#tab_logic').append('<tr id="addr' + (i + 1) + '"></tr>');
    // $('#cashacc_sel').append($('#dynamic_sel').html());
    $("select[name='cashpayment[" + i + "].name']").html($('#cashacc option:not(:selected)'));
    $("#cashacc").html($('#cashacc_sel').html());
    $("#cashacc").val(oSelectedValue);
    i++;

  });
  $("#delete_row").click(function() {
    if (i > 1) {
      $("#addr" + (i - 1)).html('');
      i--;
    }
  });

});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group col-4" style="margin-bottom: 20px;">
  <label class="col-sm-12 control-label p-sm-0 input-group">Cash Account :</label>
  <select class="form-control selectsch_items" name="cashacc" id="cashacc" required>
    <option value="">Choose an items</option>
    <option value="acc1">Account 1</option>
    <option value="acc2">Account 2</option>
    <option value="acc3">Account 3</option>
  </select>
</div>

<div class="container">
  <div class="row clearfix">
    <div class="col-md-12 column">
      <table class="table table-bordered table-hover" id="tab_logic">
        <thead>
          <tr style="background-color: #680779; color: #fff;">
            <th class="text-center">
              Account Code
            </th>
            <th class="text-center">
              A/c Name*
            </th>
          </tr>
        </thead>
        <tbody>
          <a id="add_row" class="btn btn-default pull-left adRow">Add Row</a><a id='delete_row' class="pull-right btn btn-default adRow" style="margin-right: 5px;">Delete Row</a>
          <tr id='addr0'>
            <td>
              <input type="text" id="cashacc_code" name='cashacc_code' placeholder='Enter A/c Code' class="form-control" />
            </td>
            <td>
              <select class="form-control" name="cashacc_sel" id="cashacc_sel">
                <option value="">Select A/c name</option>
                <option value="1">Plumz</option>
                <option value="2">Plumz 2</option>
                <option value="3">Plumz 3</option>
              </select>
            </td>
          </tr>
          <tr id='addr1'></tr>
        </tbody>
      </table>
    </div>
  </div>
</div>

最佳答案

我认为您需要这样的东西。



$(document).ready(function() {
  $("#cashacc_sel").html($('#cashacc').html());
  var i = 1;
  $("#add_row").click(function() {
    var oSelectedValue = $('#cashacc').val();
    $('#addr' + i).html("<td><input name='cashpayment[" + i + "].name' type='text' placeholder='Enter code' id='cashacc_code' class='sel_text form-control input-md'/></td><td><select class='sel_sel cashacc_sel form-control input-md' name='cashpayment[" + i + "].name' id='dynamic_sel'><option>Choose an items</option></select></td>");
    // {/* <td>" + (i + 1) + "</td> */}
    $("#cashacc_sel").find("select").append().appendTo($("#dynamic_sel"));
    $('#tab_logic').append('<tr id="addr' + (i + 1) + '"></tr>');
    // $('#cashacc_sel').append($('#dynamic_sel').html());
    $("select[name='cashpayment[" + i + "].name']").html($('#cashacc option:not(:selected)'));
    $("#cashacc").html($('#cashacc_sel').html());
    $("#cashacc").val(oSelectedValue);
      bindScript();
    i++;

  });
  $("#delete_row").click(function() {
    if (i > 1) {
      $("#addr" + (i - 1)).html('');
      i--;
    }
  });
    bindScript();
});


function bindScript() {
  $(document).find('.sel_sel').on("change", function () {
      $(this).parent().parent().find('.sel_text').val($(this).val());
  })
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div class="form-group col-4" style="margin-bottom: 20px;">
  <label class="col-sm-12 control-label p-sm-0 input-group">Cash Account :</label>
  <select class="form-control selectsch_items" name="cashacc" id="cashacc" required>
    <option value="">Choose an items</option>
    <option value="acc1">Account 1</option>
    <option value="acc2">Account 2</option>
    <option value="acc3">Account 3</option>
  </select>
</div>

<div class="container">
  <div class="row clearfix">
    <div class="col-md-12 column">
      <table class="table table-bordered table-hover" id="tab_logic">
        <thead>
          <tr style="background-color: #680779; color: #fff;">
            <th class="text-center">
              Account Code
            </th>
            <th class="text-center">
              A/c Name*
            </th>
          </tr>
        </thead>
        <tbody>
          <a id="add_row" class="btn btn-default pull-left adRow">Add Row</a><a id='delete_row' class="pull-right btn btn-default adRow" style="margin-right: 5px;">Delete Row</a>
          <tr id='addr0'>
            <td>
              <input type="text" id="cashacc_code" name='cashacc_code' placeholder='Enter A/c Code' class="form-control sel_text" />
            </td>
            <td>
              <select class="form-control sel_sel" name="cashacc_sel" id="cashacc_sel">
                <option value="">Select A/c name</option>
                <option value="1">Plumz</option>
                <option value="2">Plumz 2</option>
                <option value="3">Plumz 3</option>
              </select>
            </td>
          </tr>
          <tr id='addr1'></tr>
        </tbody>
      </table>
    </div>
  </div>
</div>

09-16 13:11