简体   繁体   English

Javascript重复.keyup(function(event)函数用于不同的输入

[英]Javascript repeated .keyup(function(event) function for different inputs

I've created a small ajax suggestion input field (located here: https://github.com/joyser/suggestion/ ) 我创建了一个小的Ajax建议输入字段(位于此处: https : //github.com/joyser/suggestion/

Javascript detects a keyup event in the suggestion box like this: Javascript在建议框中检测到一个keyup事件,如下所示:

$("#suggestiveInput").keyup(function(event){

})

Where the input has an id of suggestiveInput. 输入的ID为uggestiveInput的地方。

I want to be able to have more than one suggestion box on the page, but would prefer not to have to use php to print $("#suggestiveInput").keyup(function(event){ for each element. 我希望页面上可以有多个建议框,但希望不必使用php为每个元素打印$(“#suggestiveInput”)。keyup(function(event){)。

So ideally, I want one javascript function that'll handle all the inputs. 因此,理想情况下,我想要一个可处理所有输入的javascript函数。

Also, I'd prefer not to capture all keystrokes and then route it to the text field if possible 另外,我不希望捕获所有击键,然后尽可能将其路由到文本字段

I've tried something like this: 我已经尝试过这样的事情:

function suggestiveInput(inputName, options){

    suggestionCount++;
    $("#suggestiveInput"+suggestionCount).keyup(function(event){

    })

})

But it doesn't solve the problem. 但这并不能解决问题。

Apply a class name to each input and then bind the keyup event to that. 将类名称应用于每个输入,然后将keyup事件绑定到该输入。 You can apply a class to multiple elements. 您可以将一个类应用于多个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM