change event not working for a textbox when the value is assigned externally

后端 未结 6 1795
臣服心动
臣服心动 2021-01-22 17:14

I have a textbox and a button. When I click the button, it sets certain value in the textbox. I want to submit the page whenever the value of the textbox is changed.

Ple

相关标签:
6条回答
  • 2021-01-22 17:34

    You're changing the value of .set programatically, so the event doesn't fire. You have to trigger it manually:

    $('.set').val('Why am I not getting the alert saying  - Changed! ').change();
    
    0 讨论(0)
  • 2021-01-22 17:41

    Programmatically changing the value of an input doesn't fire its change event. You'll have to do that yourself using .trigger('change') (or just .change()) at the same time as changing its value:

    $('.set').val('Why am I not getting the alert saying  - Changed! ').trigger('change');
    
    0 讨论(0)
  • 2021-01-22 17:43

    You need to do this, when you are setting or changing the value of the text input chain it with .change() too:

     $("#click").click(function () {
        $('.set').val('Why am I not getting the alert saying  - Changed! ').change();
      }); //-------------------------------------this one here-------------^^^^^^^^^
    

    So whenever you set the input value this way the change event will be triggered right after click.


    May be this could help

    $("input").on('change keyup', function () {
        alert("Changed!");
    });
    
    0 讨论(0)
  • $(document).ready(function () {
        $("input").change(function () {
            Submitform();//Function for submitting form
        });
        $("#click").click(function () {
            $('.set').val('Why am I not getting the alert saying  - Changed! ');
            Submitform();//Function for submitting form
        });
    });
    

    Function for submitting the form

    function Submitform(){
    $("#YourformId").submit();//Submits your form
    }
    
    0 讨论(0)
  • 2021-01-22 17:51

    you can achieve like

    $('.element').val('value').change(function(){
          $("#form").submit();
    }); 
    

    http://jsfiddle.net/senstar2/CHjL5/5/

    0 讨论(0)
  • 2021-01-22 17:52

    Fire the event manually after setting the value using the change() function.

    $(document).ready(function () {
        $("input").change(function () {
            alert("Changed!");
        });
        $("#click").click(function () {
            $('.set').val('Why am I not getting the alert saying  - Changed! ');
            $('.set').change();  //Manual fire of event.
        });
    });
    

    Working Example http://jsfiddle.net/RUdu2/

    0 讨论(0)
提交回复
热议问题