如何从表中的单元格获取值? [英] How can I get the value from a cell in a table?

查看:38
本文介绍了如何从表中的单元格获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了所有相关文章,并没有成功使用它们.显然,我不是JavaScript专家.我曾经尝试过使用option和selectedItem,但不知道如何提取select对象以使用它.

I have read all the related posts and used them with no success. Obviously I'm not a javascript expert. I had tried with option, and selectedItem, but don't know how to extract the select object to use it.

这只是我代码的一小部分,希望您能对我有所帮助.

This is a fraction of my code, I hope you can help me.

<div class="card-body">
  <table class="table" id="products_table">
    <thead>
    <tr>
      <th>Descripción</th>
      <th>Suaje</th>
      <th>Cantidad/etiq.</th>
      <th>Importe</th>
      <th>Sustrato</th>
      <th>Sustrato $</th>
      <th>Acabado</th>
      <th>Acabado $</th>
      <th>Otros</th>
      <th>Otro $</th>
      <th>Colores</th>

    </tr>
    </thead>
    <tbody>

    <input type="text" id="test" name="test" class="form-control"/>

    <tr id="product0">


      <td>
        <input type="text" name="descripcion" class="form-control" value=""/>
      </td>
      <td>
        <select id="suajes_lista" name="suajes_lista" class="form-control" style="width: 100px;">
          <option value="">-- escoge el suaje --</option>
          @foreach ($suajes as $suaje)
            <option value="{{ $suaje->id }}">
              {{ $suaje->codificacion . ', Corte '. $suaje->corte->nombre. ', Dientes '. $suaje->dientes . ', M. Eje '. $suaje->medida_eje . ', M. Desarrollo '. $suaje->medida_desarrollo . ', C. Eje '. $suaje->no_cavidades_eje . ', C. Desarrollo '. $suaje->no_cavidades_desarrollo . ', Sep. C. Eje '. $suaje->sep_cavidades_eje. ', Sep. C. Desarrollo '. $suaje->sep_cavidedes_desarrollo . ', P. Dist. '. $suaje->porcentaje_dist . ', Ancho mm '. $suaje->ancho_papel_mm . ', Mult. ' . $suaje->mult_venta_millares
              }}
            </option>
          @endforeach
        </select>
      </td>
    </tr>
    </tbody>
  </table>
</div>

文件末尾的Javascript

Javascript at the end of file

///Agrega o elimina renglones,斯科蒂亚西翁党(partidas de lacotización)

//Agrega o elimina renglones, partidas de la cotización

let row_number = 1;
$("#add_row").click(function(e){
  e.preventDefault();
  let new_row_number = row_number - 1;
  $('#product' + row_number).html($('#product' + new_row_number).html()).find('td:first-child');
  $('#products_table').append('<tr id="product' + (row_number + 1) + '"></tr>');

  document.getElementById("products_table").rows[row_number + 1].cells[1].addEventListener("change", attachOnChangeToCells);
  row_number++;
});

function attachOnChangeToCells()
{
  $('#test').val(row_number);
  var array = @json($suajes);
  alert(document.getElementById("products_table").rows[this.parentNode.rowIndex].cells[1].innerHTML);
  alert($(this).text());
  alert($(this).val());
  alert(document.getElementById("products_table").rows[this.parentNode.rowIndex].cells[1].firstChild.value);
}

推荐答案

我看到您已经在代码中使用了 JQuery 函数,并且就 Laravel 而言,您也可以使用 JQuery 进行管理.我做了一些更改,因此它应该可以正常工作.这是示例:

I see you are already using JQuery function in the code, and as far as Laravel use that as well you can manage this with JQuery. I did some changes so it should work as desired. Here is the example:

$("#add_row").on('click', function(e){
  let row_number = $('#products_table tbody tr').length;
  e.preventDefault();
  $('#products_table').append('<tr id="product' + row_number + '"></tr>');
  $('#product' + row_number).html($('#product' + (row_number - 1)).html());
    
});

$("#products_table").on('change', '#suajes_lista', function(e){
   $("#test").val(this.value); //get current selected option value
   alert('Row where the select was triggered has ID: ' + $(this).parent().parent().attr('id')); //get up on 1 level and get parent ID
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="card-body">
  <input type="text" id="test" name="test" value="" class="form-control"/>
  <button type="submit" id="add_row">Add new</button>
  <table class="table" id="products_table">
    <thead>
        <tr>
          <th>Descripción</th>
          <th>Suaje</th>
        </tr>
    </thead>
    <tbody>
      <tr id="product0">
        <td>
          <input type="text" name="descripcion" class="form-control" value=""/>
        </td>
        <td>
          <select id="suajes_lista" name="suajes_lista" class="form-control" style="width: 100px;">
            <option value="">-- Select --</option>
            <option value="1">1</option>
            <option value="2">2</option>
          </select>
        </td>
      </tr>
    </tbody>
  </table>
</div>

希望它可以帮助您理解概念.

Hope it will help you understand the concept.

这篇关于如何从表中的单元格获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆