keyup not working in IE 8

2019-08-10 02:15发布

问题:

I just realized that this piece of code works well in Firefox but not in IE 8. I need to automatically populate the list when the user enters at least 8 characters in the input field.

$('#inputField').keyup(function (event) { 
    var $inputField = $(this); 
        if ($inputField.val().length >= 8) { popularListBox(); } 
});

function populateListBox(){
    $.get("Default.aspx?name=test", function(data) {
        $('#listBox').children().remove();
        var options = data;
        $("#listBox").html(data);
    });
}

回答1:

You want to detect the change in input field and then do some actions, right?

I think you may detect the changes instead of keyboard actions only. For example, how about if the user paste from clipboard?

Please try these codes:

$('#inputField').bind('propertychange input paste', function() {
  // do poppularListBox()
});

It works for most input field including textarea. Please check jQuery site for more information.

In my experience, .keyup() and .keypress() often get errors in IE. I would like to use .keydown() if possible (case by case)