如何为特定列上的每个表单元格使用Jquery Mask Money?

我想在特定列的每个表单元内实现Jquery Mask Money。下面是我想要实现的代码:

HTML表格代码


<script src="js/jquery.maskMoney.js"></script>

  <div class="container">
  <table id="users_data" class="table table-striped table-bordered" >
  <tr>
    <th>Name</th>
    <th>product</th>
    <th>Price</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Beer</td>
    <td ><input type="text" id="price" value="94"></td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Bread</td>
    <td ><input type="text" id="price" value="34"></td>
  </tr>
</table>
  </div>

<script>
  $('#users_data tr').each(function() {

 $("#price").maskMoney({prefix:'R$ ',allowNegative: false,thousands:'.',decimal:',',affixesStay: true});

});
  </script>

上面的代码显示带有相应数据的表,但是MaskMoney插件仅在第一个单元格内起作用。我尝试了jQuery each。()函数来执行此操作,但没有响应。在这种情况下,如何改善我的代码来解决这个问题?谢谢。

wow_365763491 回答:如何为特定列上的每个表单元格使用Jquery Mask Money?

使用jQuery选择器时,zsh表示#。页面上只能有一个带有该id值的控件。因此,您需要具有唯一的id或使用id属性。通常使用class属性。

Class

对于您的js, <tr> <td>Jill</td> <td>Beer</td> <td ><input type="text" id="price-1" class="price" value="94"></td> </tr> <tr> <td>Eve</td> <td>Bread</td> <td ><input type="text" id="price-2" class="price" value="34"></td> </tr> 只需更改为$("#price"),因为$(".price")告诉jQuery查找具有名为“价格”的类属性的控件。

本文链接:https://www.f2er.com/2911801.html

大家都在问