jQuery get input value after keypress

后端 未结 9 1636
野趣味
野趣味 2020-11-28 21:40

I have the following function:

$(document).ready(function() {
    $(\"#dSuggest\").keypress(function() {
        var dInput = $(\'input:text[name=dSuggest]\'         


        
9条回答
  •  情话喂你
    2020-11-28 22:07

    This is because keypress events are fired before the new character is added to the value of the element (so the first keypress event is fired before the first character is added, while the value is still empty). You should use keyup instead, which is fired after the character has been added.

    Note that, if your element #dSuggest is the same as input:text[name=dSuggest] you can simplify this code considerably (and if it isn't, having an element with a name that is the same as the id of another element is not a good idea).

    $('#dSuggest').keypress(function() {
        var dInput = this.value;
        console.log(dInput);
        $(".dDimension:contains('" + dInput + "')").css("display","block");
    });
    

提交回复
热议问题