4

Всем привет!

У меня есть два <input>-а. В один я ввожу цифру, во втором должен показывается результат умножения или деления этой цифры на заранее заданное число.

Как можно организовать динамический пересчёт прямо во время набора текста, без нажатия на какие-либо GUI-кнопки?

2 ответа 2

5

(function($) {
  $(function() {
    var argument = $('input[name="argument"]')
      , result = $('input[name="result"]')
      , multiplier = 4;
    argument.on('input', function() {
      result.val($(this).val() * multiplier);
    });
  });
})(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" name="argument"/>
<input type="number" name="result"/>

4

Вариант на чистом JavaScript:

const input = document.getElementById("inpt");
const inputResult = document.getElementById("res");

input.addEventListener("keyup", function() {
    inputResult.value = input.value * 4;
});
<input type="number" name="argument"/ id="inpt">
<input type="number" id="res" name="result"/>

Вариант с JQuery:

$('#inpt').on('keyup', () => {
  $('#res').val($('#inpt').val() * 4);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="number" name="argument" id="inpt">
<input type="number" id="res" name="result" />

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.