简体   繁体   English

Javascript onkeyup用户定义函数

[英]Javascript onkeyup user defined function

I did well so far, but i am not able to implement this into an html object. 到目前为止,我做得很好,但是我无法将其实现为html对象。

http://jsfiddle.net/fkyk2b8y/48/ http://jsfiddle.net/fkyk2b8y/48/

Let's assume i have an html element like <input type='text' id="textareaID" onkeyup="myFunction(this.id)" /> and i'd like to implement like this : 假设我有一个像<input type='text' id="textareaID" onkeyup="myFunction(this.id)" />这样的html元素,并且我想这样实现:

function myFunction(elementID){
   $('#'+elementID).keypress(function (e){
      ...
      ...
      ...
   });
}

It doesn't work properly after implementation as you can try from the fiddle demo. 实施后它无法正常工作,您可以从小提琴演示中尝试一下。

Thanks in advance. 提前致谢。

---UPDATE--- -更新-

I have many html outputs and they all have different ID values. 我有很多HTML输出,它们都有不同的ID值。 I want to use same function for all of these html inputs. 我想对所有这些html输入使用相同的功能。 I don't know how to implement, otherwise JS side is working quite well. 我不知道如何实现,否则JS方面运行良好。

you'r trying to put jQuery keypress event inside onkeyup function event... 您正在尝试将jQuery keypress事件放入onkeyup函数事件中...

just do that this way: 只是这样做:

<input type='text' id="textareaID" />

and inside the : 在里面:

 $("textareaID").keypress(function (e){
    if ( e.keyCode == (whatever you want) ) ...
 });

To be able to apply a function to all desired inputs, in order to achieve reusability, you can use a class instead of an id . 为了能够对所有需要的输入应用函数,以实现可重用性,可以使用class而不是id So change <input id="textareaID" ... /> to <input class="textarea" ... /> 因此,将<input id="textareaID" ... />更改为<input class="textarea" ... />

Now define make your anonymous function a named one outside the event handler binding. 现在定义使您的匿名函数在事件处理程序绑定之外成为一个命名函数。 So instead of: 所以代替:

$('#textareaID').keypress(function(e){ ... });

You'd have: 您将拥有:

function reusableFunction(e){ ... }

$('.textarea').keypress(reusableFunction);

Also in your jsFiddle you need to replace a few document.getElementById 's for this . 另外,在您的jsfiddle你需要更换一些document.getElementById的为this Here's your jsFiddle with said modifications . 这是经过修改jsFiddle Notice I have 3 inputs like the original and if I want more I can just add the class . 注意,我有3个输入,例如原始输入,如果需要更多输入,可以添加class

I'd love to update my answer if I missed something, you don't mention why your function is broken and is not immediately obvious. 如果我错过了一些事情,我很乐意更新我的答案,您不用说为什么您的功能坏了,而且不是立即显而易见的。

Let me understand this, do you want to call a jQuery function from a javascript function while passing the element's id? 让我明白这一点,您是否要在传递元素ID时从javascript函数调用jQuery函数? And then use both the element's id and the keypress event?? 然后同时使用元素的ID和keypress事件? If so, it could be done like this, 如果是这样,可以这样做

<input type="text" onkeypress="callKeyPress(this.id);" id="textId" />

    <span id="keypressCode">0</span>

<script>

$(document).ready(function() {
    alert("function is ready to use now");
});



$.fn.keyPressFunction = function(id){
    alert("id is ---> "+id);

var keycode = (event.keyCode ? event.keyCode : event.which);
        $('#keypressCode').html(keycode);

    //do something with key code

}

function callKeyPress(id){
    alert(id);
    $.fn.keyPressFunction(id);

}
</script>

Is this what you want? 这是你想要的吗?

Html HTML

<input type='text' id="textareaID1" />
<input type='text' id="textareaID2" />
<span></span>

Js s

$('input[type=text]').on('keyup',function(e){
    $('span').html(this.id);
});

you stated in your comment that there can be many inputfields with different ids, so you can do this 您在评论中表示,可能有许多输入字段具有不同的ID,因此您可以执行此操作

Demo 演示版

http://jsfiddle.net/fkyk2b8y/50/ http://jsfiddle.net/fkyk2b8y/50/

First of all, thank you for your patience and sharing information. 首先,感谢您的耐心配合和分享信息。 This was what i really meant to do : 这就是我真正想要做的:

Html part : HTML部分:

<input type='text' id="textareaID" onkeypress="currencyChecker(event, this.id)" />
<span></span>

JS part: https://jsfiddle.net/fkyk2b8y/54/ JS部分: https//jsfiddle.net/fkyk2b8y/54/

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

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