JavaScript 在 table 中添加另一个输入后,mask money 不起作用

JavaScript mask money doesn't work after adding another input in table

我是JavaScript和jQuery的新人,在使用掩码钱插件的时候遇到了问题。它运行良好,但是当我制作一些 JavaScript 以在 table 中添加另一个输入时,新行不适用于 maskmoney,即使它具有相同的 class

这是我的 table:

<thead>
    <tr>
        <td width="100"><center>Waqaf</center></td>
        <td width="100"><center>Operasional Sekolah</center></td>
        <td width="100"><center>Seragam</center></td>
    </tr>  
</thead>
<tbody>
    <tr>
        <td width="100"><input type="text" class="form-control price" name="waqaf" id=""></td>
        <td width="100"><input type="text" class="form-control price" name="operasional_sekolah" id=""></td>
        <td width="100"><input type="text" class="form-control price" name="seragam" id=""></td>
    </tr>
</tbody>  

用于添加新字段的 JS 是 运行 好吧,当我用相同的 class 检查检查元素输出输入时。问题出在插件 JS mask money

<script type="text/javascript">
    $('input.price').number( true, 2 );
</script>

(见下图)

但是当我手动输入另一个文本时效果很好

为什么即使我使用具有相同输入和 class 的 js 事件创建另一个字段,它也不起作用?

你应该这样做 $('input.price').number( true, 2 );每次创建新行时,该方法目前仅适用于第一行。

如果您指的是 this plugin, then it's important to know it uses $.bind to handle events. It means that new inputs won't be handled in any way by this plugin. Today we should use $.on 这种绑定(以及 jQuery 早期版本中的“$.live”)。