0

Как изменять значение в input=text value после нажатие на кнопку button

То есть имеются несколько кнопок, button A button B button C и одно поле, после нажатие на кнопку, меняется значение в поле input type=text

Вот пример мой с radio работает, а с кнопкой button - как сделать?

$('input.trip').on('change', function() {
    $('#my_field').val( $(this).val() );
});

$('input.but').on('change', function() {
    $('#my_field2').val( $(this).val() );
});

$('input.but').on('change', function() {
    $('#my_field3').val( $(this).val() );
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="radio" class="trip" name="rad" value="a">A
<input type="radio" class="trip" name="rad" value="b">B
<input type="radio" class="trip" name="rad" value="c">C

<input type="text" id="my_field" value="">


<input type="submit" class="sub" value="a">
<input type="submit" class="sub" value="b">
<input type="submit" class="sub" value="c">

<input type="text" id="my_field2" value="">


<button class="but" class="but" value="a">A</button>
<button class="but" class="but" value="b">B</button>
<button class="but" class="but" value="c">C</button>

<input type="text" id="my_field3" value="">

Вот кнопки button и submit не работает Или я что то не так делаю ?

1 ответ 1

3

Так хотели?

$('input.trip').on('change', function() {
    $('#my_field').val( $(this).val() );
});

$('input.sub').on('click', function() {
    $('#my_field2').val( $(this).val() );
});

$('button.but').on('click', function() {
    $('#my_field3').val( $(this).val() );
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="radio" class="trip" name="rad" value="a">A
<input type="radio" class="trip" name="rad" value="b">B
<input type="radio" class="trip" name="rad" value="c">C

<input type="text" id="my_field" value="">


<input type="submit" class="sub" value="a">
<input type="submit" class="sub" value="b">
<input type="submit" class="sub" value="c">

<input type="text" id="my_field2" value="">


<button class="but" value="a">A</button>
<button class="but" value="b">B</button>
<button class="but" value="c">C</button>

<input type="text" id="my_field3" value="">

Ваш ответ

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

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