jQuery查找数据属性值

时间:2016-10-29 21:11:32

标签: jquery html5

你好我有一个带有数据属性的表,如果data属性具有相同的值,我会找到什么。

 <tr style="height: 40px;" cart-item="" data-line-weight="10" data-total-lien="10" data-item-barcode="3434" class="tablerow" id="rowid1">

我试图找到数据项条形码相等的TR&#34; 3434&#34;  并更改找到的TR内输入字段的值

<tr style="height: 40px;" cart-item="" data-line-weight="10" data-total-lien="10" data-item-barcode="3434" class="tablerow" id="rowid1">
<td width="210" class="text-left" style="line-height:30px;">
<span style="text-transform: uppercase;">food</span></td>
<td width="130" class="text-center" style="line-height:30px;">10</td>
<td width="145" class="text-center"><div class="input-group input-group-sm">
<span class="input-group-btn">
<button class="btn btn-default item-reduce" type="button" id="reduce1">-</button>
</span>
<input type="number" name="QNY[]" value="1" class="form-control qny" aria-describedby="sizing-addon3" id="qny1">
<span class="input-group-btn"><button class="btn btn-default item-add" type="button" id="addB1">+</button></span></div></td></tr>

由于

2 个答案:

答案 0 :(得分:1)

如果我很清楚你想要实现的目标应该有效:

$('tr[data-item-barcode="3434"] input[type="number"]').val('YOUR VALUE');

答案 1 :(得分:0)

我不太确定你想要实现什么,但我猜它是这样的,如果我将tr与该属性匹配(data-item-barcode = 3434)我想用一些值更新输入?

所以,在这里:

&#13;
&#13;
var trMatched = $("tr[data-item-barcode=3434]");
if(trMatched.length > 0){
  $("input", trMatched).val(20);
  }
&#13;
<!DOCTYPE html>
<html>

<head>
  <title>Test</title>
</head>

<body>
  <table>
  <tr style="height: 40px;" cart-item="" data-line-weight="10" data-total-lien="10" data-item-barcode="3434" class="tablerow" id="rowid1">
    <td width="210" class="text-left" style="line-height:30px;">
      <span style="text-transform: uppercase;">food</span>
    </td>
    <td width="130" class="text-center" style="line-height:30px;">10</td>
    <td width="145" class="text-center">
      <div class="input-group input-group-sm">
        <span class="input-group-btn">
<button class="btn btn-default item-reduce" type="button" id="reduce1">-</button>
</span>
        <input type="number" name="QNY[]" value="1" class="form-control qny" aria-describedby="sizing-addon3" id="qny1">
        <span class="input-group-btn"><button class="btn btn-default item-add" type="button" id="addB1">+</button></span>
      </div>
    </td>
  </tr>
</table>

</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

</html>
&#13;
&#13;
&#13;