Select the changes of a textbox that was disabled from code behind in jQuery

时间:2015-09-14 15:54:00

标签: javascript jquery asp.net vb.net

I have a page with a asp:textBox. In my code behind (VB.net) in the Page Load, depending on such filters, i'm going to enable/disable this asp:textBox in this way:

this.txtB.Enabled = true;
this.txtB.Enabled = false;

In the moment that the user change the context of this asp:Textbox (only if it's enabled) i want to do some stuff, and so i thought to use jQuery in this way:

 $('.txtB:enabled').on("propertychange input paste", function () { ... });

The problem is that, in some cases, the textBox that in the beginning was disabled had to become enabled too. And i do that always in jQuery:

$(".txt_PISocAppNom").prop('disabled', false);

But in this case when then the user change the context of the textBox, the previous method didn't catch the changes.

I assume that the problem derive from the fact that in my code behind i'm setting the ENABLE attribute and in jQuery the DISABLE attribut.

I don't know if my supposition is correct and in that case how to change the code to make it work.

2 个答案:

答案 0 :(得分:1)

您是否尝试过使用' live'而不是'?

$('.txtB:enabled').live("propertychange input paste", function () { ... });

如果您的文本框在开头被禁用,则选择器' .txtB:已启用'不会申请,所以变更事件不会附加到它。使用' live'将确保该事件也为未来元素附加。

'活'已在新版本的jquery中弃用。在jquery 1.7+中,您还可以使用$('.parentB').on("propertychange input paste", ".txtB:enabled", function () { ... });获得与' live'相同的结果。我正在使用' .parentB'作为获取文本框父级的选择器。

答案 1 :(得分:0)

问题是我插入了

 $(".txtB:enabled").on("propertychange input paste", function () { ... });

中的

 $(document).ready(function () { ... });

在这种情况下,它仅在早期阶段受到攻击,只能启用那些项目。因此,如果我的文本框最初被禁用,并且仅在稍后启用,则它不具有.on(...)属性。