$(this). val(') does not remove cache information

Asked

Viewed 14 times

-1

The idea is that I type and if the value is greater than the specified amount then it leaves the form blank, until it is all ok, however, when giving an Alert the value remains stored... What can it be?

var quantidade = 100;
$('input').keyup(()=>{
            if ($('input').val() > quantidade && $('input').val().length == quantidade.length || $('input').val().length > quantidade.length)
            {
              $('input').val('');
            }});
            
$('button').click(()=>{ alert($('input').val()) })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>


<input type="number" />
<button>Clicar</button>

  • You asked a question these days with that same code and I repeat, your condition on if is wrong, there is no property length for type number!

1 answer

0


it is your validation that this wrong, try so:

var quantidade = 100;
$('input').keyup(()=>{
            if ($('input').val() > quantidade && $('input').val().length > 0)
            {
              $('input').val('');
            }});
            
$('button').click(()=>{ alert($('input').val()) })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>


<input type="number" />
<button>Clicar</button>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.