Fire onchange event for TextBox when readonly is true

前端 未结 4 1716
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-19 03:35

I am trying to have some functionality on change of a textbox which is readonly. But when I am trying to update the textbox using javascript, the change event is not firing

相关标签:
4条回答
  • 2021-01-19 03:38

    Well you have to do this way: http://jsfiddle.net/kmvSV/1/

     $(document).ready(function () {
       $('input[id$=_txtTest]').bind("change", function () {
         alert($(this).val());
       });
       $('button').bind("click", function () {
         $('input[id$=_txtTest]').val('hello').trigger('change');
       });
     });
    
    0 讨论(0)
  • 2021-01-19 03:52

    you can do like this

    function setValueOfTextBox()
    {
        var myElement = document.getElementById("textboxid");
        myElement.value = "hello";
        //following code fire change event for you text box
        if (myElement.onchange) 
             myElement.onchange();
    }
    
    0 讨论(0)
  • 2021-01-19 03:56

    I believe that for some security reason, the events are not fired. But you can achieve this by triggering the specific event on that element. E.g. $('input[id$=_txtTest]').trigger('change');

    I hope this helps someone.

    0 讨论(0)
  • 2021-01-19 03:57

    The change event is triggered by real user events only not javascript actions.

    You may trigger the change event, like so:

    $('input[id$=_txtTest]').val('hello').change();
    
    0 讨论(0)
提交回复
热议问题