使用 jQuery / javascript 启用(但不触发)RequiredValidator

发布于 2024-11-15 12:13:20 字数 1071 浏览 3 评论 0原文

我有一个可能有 10 个字段的表单。其中一个字段是一个复选框,默认情况下未选中,并且表单上的许多字段仅在选中该框时才启用+必需。我已经成功地找到了如何调用 ValidatorEnable(requiredFieldValidator, true) 来处理这个问题(我已经检查了许多关于该主题的 StackOverflow 问题)。

 function toggleStatus() {
          if ($('#ctl00_main_chkContactMe').is(':checked')) {
              $('#elementsToOperateOn :input').removeAttr('disabled');
              $('#elementsToOperateOn label').removeClass('off');
              ValidatorEnable($("[id$=RequiredFieldValidator1]")[0], true);
              ValidatorEnable($("[id$=RequiredFieldValidator2]")[0], true);
          } else {
              $('#elementsToOperateOn :input').attr('disabled', true);
              $('#elementsToOperateOn label').addClass('off');
              ValidatorEnable($("[id$=RequiredFieldValidator1]")[0], false);
              ValidatorEnable($("[id$=RequiredFieldValidator2]")[0], false);
          }
      }

但是,我遇到了一个尚未解决的问题。当我的用户选中该框并且这些字段现已启用且为必填字段时,我的验证器会立即触发其“此字段为必填字段”消息。这是在用户有机会输入任何内容之前,所以这不是一个很好的用户体验。我可以告诉验证器什么,以便它知道,即使我启用它,“该字段还没有获得焦点,所以在有人故意将其留空之前不要显示错误消息”?

I have a form with maybe 10 fields. One of those fields is a checkbox, default unchecked, and a number of the fields on the form are only enabled+required if that box is checked. I've successfully found out how to invoke ValidatorEnable(requiredFieldValidator, true) to handle this (I've checked out numerous StackOverflow questions on the subject).

 function toggleStatus() {
          if ($('#ctl00_main_chkContactMe').is(':checked')) {
              $('#elementsToOperateOn :input').removeAttr('disabled');
              $('#elementsToOperateOn label').removeClass('off');
              ValidatorEnable($("[id$=RequiredFieldValidator1]")[0], true);
              ValidatorEnable($("[id$=RequiredFieldValidator2]")[0], true);
          } else {
              $('#elementsToOperateOn :input').attr('disabled', true);
              $('#elementsToOperateOn label').addClass('off');
              ValidatorEnable($("[id$=RequiredFieldValidator1]")[0], false);
              ValidatorEnable($("[id$=RequiredFieldValidator2]")[0], false);
          }
      }

However, I'm having a problem I don't yet see addressed. When my user checks the box and the fields are now enabled and required, my validator immediately fires its "This field is required" message. This is before the user's even had a chance to type anything, so it's not a very good user experience. What can I tell the Validator so that it knows, even though I'm enabling it, "This field hasn't had the focus at all yet, so don't show an error message until somebody has deliberately left it empty"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月静好 2024-11-22 12:13:20

我遇到了同样的问题,并使用以下代码解决了它:

var myValidator = document.getElementById('<%=myValidator.ClientID %>');
ValidatorEnable(myValidator, true);
myValidator.isvalid = true;
ValidatorUpdateDisplay(myValidator);

第三行可能看起来有点奇怪,但验证器稍后将在任何更改或在页面外发布时进行验证。

你也可以尝试这个功能:

function myValidatorEnabler(validator, enable) {   
    validator.enabled = enable;
    ValidatorUpdateDisplay(validator);
}

I had the same issue and solved it using this code:

var myValidator = document.getElementById('<%=myValidator.ClientID %>');
ValidatorEnable(myValidator, true);
myValidator.isvalid = true;
ValidatorUpdateDisplay(myValidator);

The third row might look a bit wierd, but the validator will later be validated on any change or on posting off the page.

You could also try this function:

function myValidatorEnabler(validator, enable) {   
    validator.enabled = enable;
    ValidatorUpdateDisplay(validator);
}
放手` 2024-11-22 12:13:20

直到提交之前才启用它们,我认为您应该能够在提交时启用它们,而不是使用使用 RegisterOnSubmitStatement 调用的 javascript 函数,据我所知,它将在验证之前触发。

http://msdn.microsoft.com/en -us/library/system.web.ui.clientscriptmanager.registeronsubmitstatement.aspx

这将使您不必回发。

Don't enable them until just before the submit, I think you should be able to enable them on the submit instead using a javascript function called by using RegisterOnSubmitStatement, it will fire before the validation afaik.

http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registeronsubmitstatement.aspx

This will save you having to post back.

故事还在继续 2024-11-22 12:13:20

默认情况下,您的必填字段验证器已启用(如果没有,则启用它)。因此,仅使用那些需要禁用它的条件,然后就完成了。

See by default your Required Field Validator is Enabled (if not then make it). So use only those condition which requires it to disable and you are done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文