Jquery How to Monitor Exchange of Values?

Asked

Viewed 870 times

3

I have a radio button:

<input type="radio" value="1" id="monitor" style="display:none;">

Here I change the value of it, and I would like an alarm to be triggered:

$(function(){

        $("#umItemQualquer").click(function(){

            alert("antes da mudança " + $("#monitor").val())

            $("#monitor").val(2); //Alterando valor do radio

            alert("depois da mudança " + $("#monitor").val())

        });

        // Essa função não deveria ser ativada?
        $("#monitor").change(function(){
            alert("agora o bicho vai pegar!")
            dispararAlarme();
        });

    });
  • According to your code, every time you change the value of the radio you will run Alert and the function will fire Larme. But what would be the purpose of this, for a radio that is hidden (display:None) on the page?

  • A Gambi I want to do here, rsrsrs...

  • 1

    With "display:None" the change will never be triggered, as there is no way to make the element lose focus if it does not appear on the screen... ;)

  • @Jedaiasrodrigues has been answered your question?

1 answer

3


The event change is only called when the field loses focus and has its value changed. More information here.

When you change the value per command, it has the modified value, but has no loss of focus, so the change is not called.

If you want to force its activation manually just put

$("#monitor").trigger("change");
  • Thank you for your reply, my friend, but I did not understand the Rigger question

  • 1

    @Jedaiasrodrigues Rigger (Translation: trigger) fires the event change of #monitor

  • 1

    @Jedaiasrodrigues You can use $("#monitor").change(); if you prefer. The trigger it’s just a shortcut.

Browser other questions tagged

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