获取父级中的输入值父级

时间:2020-08-26 14:07:02

标签: javascript jquery

我有一个包含几行表格的页面(已生成HTML,我无法对其进行修改)。在每一行中,有2个输入(不同的列)。当我修改其中一个输入的值(带有“ qty”类名)时,我想检索第二个输入的值,但是我做不到。请注意,行数不是固定的

我生成的html并尝试

jQuery('.qty').on('change', function() {
  console.log(jQuery(this).val());
  console.log($(this).closest("tr").children('.product_quantity').children('input').val());
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
```
<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Price</th>
      <th class="product-quantity">Quantity 1</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="product-name">Name product 1</td>
      <td>$1,00</td>
      <td class="product-quantity">
        <input type="number" min="1" step="1" name="items[45955][quantity]" value="4">
      </td>
      <td>
        <div>
          <input type="number" class="qty" step="1" min="0" max="" name="quantity" value="1" title="Qté" size="4" placeholder="" inputmode="" kl_vkbd_parsed="true">
        </div>
      </td>
    </tr>
    <tr>
      <td class="product-name">Name product 1</td>
      <td>$1,00</td>
      <td class="product-quantity">
        <input type="number" min="1" step="1" name="items[45955][quantity]" value="4">
      </td>
      <td>
        <div>
          <input type="number" class="qty" step="1" min="0" max="" name="quantity" value="1" title="Qté" size="4" placeholder="" inputmode="" kl_vkbd_parsed="true">
        </div>
      </td>
    </tr>
  </tbody>
</table>

我试图通过父母和其他许多测试来改变最接近的距离,但是我做不到 你能帮我吗?

2 个答案:

答案 0 :(得分:0)

我假设您要在更改数量值时获取操作值

尝试一下

$('.product-quantity input').on('keyup', function () {
    console.log($(this).val());
    console.log($(this).parent('td').next('td').find('div input').val());
})

答案 1 :(得分:0)

尝试一下:

<Row className="form-group">
   <Col md={{size: 3, offset: 3}}>
       <div className="form-check">
          <Label check>
             <Control.checkbox
              className="form-check-input" 
              model=".agree" 
              name="agree"
              id="agree" 
             />May We Contact You?
          </Label>
       </div>
   </Col>
   <Col md={6}>
       <Control.select
        className="form-control"
        model=".type"
        name="type"
        id="type"
        >
           <option value="1">Tel.</option>
           <option value="2">Email</option>
        </Control.select>
   </Col>
</Row>