开发者

Can a change, to a hidden input, by jQuery be listened by any event?

开发者 https://www.devze.com 2023-04-10 11:01 出处:网络
Maybe you understand also from the question, I am trying开发者_Go百科 to trigger one change that happens to a hidden input.

Maybe you understand also from the question, I am trying开发者_Go百科 to trigger one change that happens to a hidden input.

<input type="hidden" name="secret" id="secret" onchange="alert(MSG);" value="0" />

Under the hood, I load some data to a div which is also hidden, where loading is done with jQuery, input value changes to 1. And the alert box doesn't show up!!

Loading html:

$('.somediv').load('ajax.php?changesecret=yes');

Change coming from php

<script type="text/javascript">
    $("#secret").val(1);
</scipt>

Any ideas, how can I trigger this change?


How about manually triggering the event after changing the value?

<script type="text/javascript">
    $("#secret").val(1).trigger('change');
</scipt>


You could handle it all in the javascript like this:

$.get('ajax.php',
    {
        changesecret: 'yes'
    },
    function (data) // This gets the data back from ajax.php
    {
        $('.somediv').html(data);
        alert(MSG);
    }
);


Any change to an input done via jQuery code wont trigger the change event unless you trigger it as well manually. Try these:

   $("#secret").val(1).trigger('change');

or

   $("#secret").val(1).change();

Excerpt from the jQuery documentation

If the field loses focus without the contents having changed, though, the event is not triggered. To trigger the event manually, apply .change() without arguments:

$('#other').click(function() {
  $('.target').change();
});
0

精彩评论

暂无评论...
验证码 换一张
取 消