最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Fire onchange event for TextBox when readonly is true - Stack Overflow

programmeradmin3浏览0评论

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 for the textbox.

<script type="text/javascript">
        $(document).ready(function () {
            $('input[id$=_txtTest]').bind("change", function () {
                alert("test");
            });
                        });
        function ChangeText() {
            $('input[id$=_txtTest]').val('hello');
         }
    </script>

I am calling ChangeText method on click of a button. But it is not firing the textchange event for the textbox.

Can anybody tell me what is wrong here?

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 for the textbox.

<script type="text/javascript">
        $(document).ready(function () {
            $('input[id$=_txtTest]').bind("change", function () {
                alert("test");
            });
                        });
        function ChangeText() {
            $('input[id$=_txtTest]').val('hello');
         }
    </script>

I am calling ChangeText method on click of a button. But it is not firing the textchange event for the textbox.

Can anybody tell me what is wrong here?

Share Improve this question asked Jan 15, 2013 at 7:30 Ashwani KAshwani K 7,98021 gold badges64 silver badges105 bronze badges 4
  • Possible duplicate: stackoverflow.com/questions/3600680/… – Aleksander Blomskøld Commented Jan 15, 2013 at 7:34
  • Also duplicate with: stackoverflow.com/questions/4672505/… – Christiaan Westerbeek Commented Jan 15, 2013 at 7:37
  • You have readonly set to true on the input box? Does the value actually change to 'hello' when you call ChangeText? – Christiaan Westerbeek Commented Jan 15, 2013 at 7:39
  • Yes, it is changed to hello, but no event is fired – Ashwani K Commented Jan 15, 2013 at 8:16
Add a comment  | 

5 Answers 5

Reset to default 9

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');
   });
 });

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();

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();
}

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.

onkeypress event or one of the several other events:

Since the read-only field doesn't really change; the onChange event doesn't trigger on it. However; there are several other events that you can listen to.

Here's a short demo:

input {
  font-size: 16px;
  line-height: 30px;
  padding: 3px;
  border: 2px darkblue solid;
}
<script>
function sayHi(evt) {

  alert('Are you tyring to edit a read-only? Click OK for details.')
  alert(JSON.stringify({
    keyCode: evt.keyCode, 
    charCode: evt.charCode, 
    altKey:evt.altKey, 
    ctrlKey: evt.ctrlKey, 
    shiftKey: 
    evt.shiftKey
  }), null, 2);
}
</script>



<input onkeypress="sayHi(event);" readonly placeholder="A readonly field..." />

发布评论

评论列表(0)

  1. 暂无评论