.change does not react as dynamically as expected

I am new to javascript, I am using jquery, this is my html with php code:

<form action="" method="POST" enctype="multipart/form-data">
    <h1>Quantity 1</h1>
    <input id="btn1" type="number" name="cantpag" value="<?php echo $cp; ?>">

    <h1>Quantity 2</h1>
    <input id="btn2" type="number" name="cantreinv" value="<?php echo $cr; ?>">

    <input type="submit">
</form>

      

Here is my JS:

<script>
$(document).ready(function(){
    $(document).on('change','#btn1',function(){
        alert('works');
    });
});</script>

      

When I change the value of "# btn1", nothing happens immediately, instead I have to click a tab or somewhere else for the warning to appear. Is there a way for this to be instantaneous, like just adding or removing a number from an input field triggers a warning?

+3


source to share


1 answer


    $(document).ready(function(){
        $(document).on('input','#btn1',function(){
            alert('works');
        });
    });
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="POST" enctype="multipart/form-data">
    <h1>Quantity 1</h1>
    <input id="btn1" type="number" name="cantpag" value="<?php echo $cp; ?>">

    <h1>Quantity 2</h1>
    <input id="btn2" type="number" name="cantreinv" value="<?php echo $cr; ?>">

    <input type="submit">
</form>
      

Run codeHide result




The event input

should perform better. Event change

for certain items requires registration blur

.

+3


source







All Articles