如何将keyup事件绑定到jQuery插件

时间:2021-12-05 20:26:49

i am trying to create jQuery plugin which needs to trigger on keyup of input tag. But, somehow its not working :(

我正在尝试创建需要在输入标记的keyup上触发的jQuery插件。但是,它不工作:(

I've tried it so far:

到目前为止我已经尝试过了:

JS:

$.fn.search_panel = function() {
    if($(this).prop("tagName").toLowerCase() == 'input'){
        var input_str = $.trim($(this).val());
        console.log($(this));

        onkeyup = function(){
            console.log(input_str);
        }
    }
};

Plugin Initialization

$(document).ready(function(){
    $('input').search_panel();
});

HTML:

<input type="text" />

From the above code, it only console when page loads for the first time, but after entering anything in input box it doesn't console.

从上面的代码中,它只在第一次加载页面时进行控制,但是在输入框中输入任何内容之后它都没有控制台。

2 个解决方案

#1


You're inadvertantly binding to the window's onkeyup event. You should use $(this).on instead to bind to the individual keyup event on each input:

你无意中绑定了窗口的onkeyup事件。您应该使用$(this).on来绑定每个输入上的单个keyup事件:

$.fn.search_panel = function() {
    // Iterate all elements the selector applies to
    this.each(function() {
        var $input = $(this);
        // Can probably make this more obvious by using "is"
        if($input.is("input")){
            // Now bind to the keyup event of this individual input
            $input.on("keyup", function(){
                // Make sure to read the value in here, so you get the
                // updated value each time
                var input_str = $.trim($input.val());
                console.log(input_str);
            });
        }
    });
};
$('input').search_panel();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input><input><input><input>

#2


Add keyup event inside plugin and bind it to current input,

在插件中添加keyup事件并将其绑定到当前输入,

$.fn.search_panel = function () {
    if ($(this).prop("tagName").toLowerCase() == 'input') {
         $(this).keyup(function () {
            var input_str = $.trim($(this).val());
            console.log($(this));
            console.log(input_str);
         });
    }  
};

Demo

#1


You're inadvertantly binding to the window's onkeyup event. You should use $(this).on instead to bind to the individual keyup event on each input:

你无意中绑定了窗口的onkeyup事件。您应该使用$(this).on来绑定每个输入上的单个keyup事件:

$.fn.search_panel = function() {
    // Iterate all elements the selector applies to
    this.each(function() {
        var $input = $(this);
        // Can probably make this more obvious by using "is"
        if($input.is("input")){
            // Now bind to the keyup event of this individual input
            $input.on("keyup", function(){
                // Make sure to read the value in here, so you get the
                // updated value each time
                var input_str = $.trim($input.val());
                console.log(input_str);
            });
        }
    });
};
$('input').search_panel();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input><input><input><input>

#2


Add keyup event inside plugin and bind it to current input,

在插件中添加keyup事件并将其绑定到当前输入,

$.fn.search_panel = function () {
    if ($(this).prop("tagName").toLowerCase() == 'input') {
         $(this).keyup(function () {
            var input_str = $.trim($(this).val());
            console.log($(this));
            console.log(input_str);
         });
    }  
};

Demo