簡體   English   中英

將 AutoNumeric.js 應用於整個類

[英]Applying AutoNumeric.js to an entire class

如果有人可以幫助我完成我認為是 AutoNumeric.js 的簡單應用程序,我會很高興。 我有以下代碼:

小提琴鏈接: https : //jsfiddle.net/yu1s9nrv/8/

<table id="shareInput" class="table_standard">
  <tr>
    <th>Name</th>
    <th>Quantity</th>
    <th>Price</th>
    <th>Growth</th>
    <th>Yield</th>

  </tr>
  <tr>
    <td><input type="text" class="input_field_large" id="shareName" value=""></td>
    <td><input type="text" class="input_field_medium_num" id="shareQty" value=""></td>
    <td><input type="text" class="input_field_medium_dollar" id="sharePrice" value=""></td>
    <td><input type="text" class="input_field_medium_pct" id="shareGrowth" value=""></td>
    <td><input type="text" class="input_field_medium_pct" id="shareYield" value=""></td>


  </tr>
  <tr>
    <td><input type="text" class="input_field_large" id="shareName" value=""></td>
    <td><input type="text" class="input_field_medium_num" id="shareQty" value=""></td>
    <td><input type="text" class="input_field_medium_dollar" id="sharePrice" value=""></td>
    <td><input type="text" class="input_field_medium_pct" id="shareGrowth" value=""></td>
    <td><input type="text" class="input_field_medium_pct" id="shareYield" value=""></td>

  </tr>
</table>

<script>
  window.onload = function() {


    const anElement = new AutoNumeric('.input_field_medium_pct', 0, {
      suffixText: "%"
    });

  };

</script>

我期望的輸出是具有類 input_field_medium_pct 的所有字段都具有所需的 AutoNumeric 格式,但是它只格式化具有該類的第一個字段。 文檔中寫道:

// AutoNumeric 構造函數類也可以接受字符串作為 css 選擇器。 QuerySelector ,它使用QuerySelector並將自身限制為僅找到它找到的第一個元素。 anElement = new AutoNumeric('.myCssClass > input'); anElement = new AutoNumeric('.myCssClass > input', { options });

取自: https : //github.com/autoNumeric/autoNumeric#initialize-one-autonumeric-object

我是 JS 新手,發現 AutoNumeric 文檔說明有點令人困惑,有沒有人遇到過這個問題或能夠解釋為什么會出現這種情況? 提前致謝。

您需要使用Autonumeric.multiple將其Autonumeric.multiple應用於多個元素。

 const anElement =  AutoNumeric.multiple('.input_field_medium_pct', 0, {
      suffixText: "%"
 });

檢查工作jsfiddle

另外,檢查文檔https://github.com/autoNumeric/autoNumeric#initialize-multiple-autonumeric-objects-at-once

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM