admin管理员组

文章数量:1321065

I have a validator in my Webform.aspx

<asp:RequiredFieldValidator ID="val1" ClientIDMode="Static" runat="server"/>

In my jquery, I have a function which attempts to disable it called by a button in the markup

btnclick = function()
{
    var a = $('#val1');
    alert(a); //alerts [object Object]
    ValidatorEnable(a, false); //gets a console error
}

when this function is executed, I get an error in the console (I am using Google Chrome), saying that uncaught TypeError: Cannot set property 'visibility' of undefined

Is this saying that my var a is undefined? ... but that makes no sense since it is alerting an [object Object]

I also did alert(a.length) which gave me 1 as expected.

I have a validator in my Webform.aspx

<asp:RequiredFieldValidator ID="val1" ClientIDMode="Static" runat="server"/>

In my jquery, I have a function which attempts to disable it called by a button in the markup

btnclick = function()
{
    var a = $('#val1');
    alert(a); //alerts [object Object]
    ValidatorEnable(a, false); //gets a console error
}

when this function is executed, I get an error in the console (I am using Google Chrome), saying that uncaught TypeError: Cannot set property 'visibility' of undefined

Is this saying that my var a is undefined? ... but that makes no sense since it is alerting an [object Object]

I also did alert(a.length) which gave me 1 as expected.

Share Improve this question edited Mar 7, 2013 at 15:20 jrummell 43.1k18 gold badges115 silver badges175 bronze badges asked Mar 7, 2013 at 15:13 RhsRhs 3,31813 gold badges52 silver badges90 bronze badges 7
  • I just had this problem yesterday and couldn't figure it out. Went with CustomValidator, that may be a viable option for you as well. – Andrew Walters Commented Mar 7, 2013 at 15:17
  • @AndrewWalters I am leaning towards that however I saw that this is a viable solution. I am just confused as to why it doesn't work. – Rhs Commented Mar 7, 2013 at 15:19
  • 1 Have you tried ValidatorEnable(a[0], false)? ValidatorEnable needs an element, not a jQuery object. – jrummell Commented Mar 7, 2013 at 15:20
  • Usually the requiredfield validators don't need to be invoked with a custom javascript, they will be invoked with a submit button. Why invoke with .js? – Jon Harding Commented Mar 7, 2013 at 15:20
  • @jrummell that appears to be the solution thanks! – Rhs Commented Mar 7, 2013 at 15:22
 |  Show 2 more ments

1 Answer 1

Reset to default 12

ValidatorEnable needs an element, not a jQuery object. You can get the first matched element using an index.

var a = $('#val1');
ValidatorEnable(a[0], false);

本文标签: javascriptEnableValidator in JSStack Overflow