Accessing jQuery function from inside formview edititemtemplate textbox

时间:2015-09-14 16:14:44

标签: jquery asp.net vb.net

I have a webform (code behind VB) with tabcontainer in it, which in turn has a tabpanel in it, which has my formview on it. I have defined a jQuery generic function. I want to access this function from my formview edititemtemplate textbox, on keydown, something like $("input[type=text]").keydown(function () {....}

Somehow this function is not getting accessed at all.

Whereas when I access this function from a textbox which is outside tabcontainer, then the function is accessible perfectly.

Can someone pls tell me how to get this function to work from formview edititemtemplate textbox?

1 个答案:

答案 0 :(得分:1)

如果没有HTML示例,这是动态添加元素问题的标准解决方案。 tabContainer可能正在动态地更改内容。

将单个委派事件处理程序附加到要侦听的元素的不变的祖先。如果没有其他更接近/方便的话,document是最好的默认值。

e.g。

$(document).on("keydown", "input[type=text]", function () {....}

这可以通过监听事件将DOM冒泡到连接元素来实现。它然后将jQuery选择器应用于bubble bubble中的元素。 然后将您的函数仅应用于导致事件的匹配元素

最终结果是元素只需要在事件时匹配,而不是在事件被注册时匹配。