admin管理员组

文章数量:1291123

I use this plugin :

And I use this initialization :

AutoNumeric.multiple('.autonumeric-number', AutoNumericConfigDecimal);
AutoNumeric.multiple('.autonumeric-integer', AutoNumeric.getPredefinedOptions().integerPos);

And then, I have an Ajax call and I need to set the new value of my input. The problem is : I can't use .set() method of the plugin because it requires to have a variable you got after initialisation.

Since I use multiple selectors, I can't have this variable.

For now, I tried to initialize again :

// element already has mask
<input id="element" class="autonumeric-integer" />

// after ajax call
var element = new AutoNumeric('#element', AutoNumericConfig);
element.set(value);

But it doubles what I write in the input, "2" bees "22".

Is it possible to use AutoNumeric functions using only the DOM element ? and not the variable you get after initialization ?

I use this plugin : https://github./autoNumeric/autoNumeric

And I use this initialization :

AutoNumeric.multiple('.autonumeric-number', AutoNumericConfigDecimal);
AutoNumeric.multiple('.autonumeric-integer', AutoNumeric.getPredefinedOptions().integerPos);

And then, I have an Ajax call and I need to set the new value of my input. The problem is : I can't use .set() method of the plugin because it requires to have a variable you got after initialisation.

Since I use multiple selectors, I can't have this variable.

For now, I tried to initialize again :

// element already has mask
<input id="element" class="autonumeric-integer" />

// after ajax call
var element = new AutoNumeric('#element', AutoNumericConfig);
element.set(value);

But it doubles what I write in the input, "2" bees "22".

Is it possible to use AutoNumeric functions using only the DOM element ? and not the variable you get after initialization ?

Share Improve this question edited Nov 29, 2018 at 15:04 Gabriele Petrioli 196k34 gold badges271 silver badges328 bronze badges asked Nov 29, 2018 at 10:47 Vincent DecauxVincent Decaux 10.7k7 gold badges63 silver badges99 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

From the docs i see that there is a static method for finding the AutoNumeric instance that handles an element, named getAutoNumericElement

So if you have the id or a reference to the node you want you can pass it to this method and then you can call the set() on it.

AutoNumeric.multiple('.autonumeric-integer', AutoNumeric.getPredefinedOptions().integerPos);


document.querySelector('button').addEventListener('click', function(){
// assuming you can target the elements based on id
  const element = AutoNumeric.getAutoNumericElement('#element3')
  element.set(13);
})
<script src="https://cdnjs.cloudflare./ajax/libs/autonumeric/4.1.0/autoNumeric.min.js"></script>

#element1: <input id="element1" class="autonumeric-integer" /><br/>
#element2: <input id="element2" class="autonumeric-integer" /><br/>
#element3: <input id="element3" class="autonumeric-integer" /><br/>
#element4: <input id="element4" class="autonumeric-integer" />

<button> update #element3 </button>

This one-liner should work for you as well.

AutoNumeric.set('#element','2');

you can use:

new AutoNumeric('#revenue', {
    decimalPlaces: 0,
    unformatOnSubmit: true
}); // 

本文标签: javascriptAutoNumericset a value after Ajax callStack Overflow