使用 jQuery / javascript 启用(但不触发)RequiredValidator
我有一个可能有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了同样的问题,并使用以下代码解决了它:
第三行可能看起来有点奇怪,但验证器稍后将在任何更改或在页面外发布时进行验证。
你也可以尝试这个功能:
I had the same issue and solved it using this code:
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:
直到提交之前才启用它们,我认为您应该能够在提交时启用它们,而不是使用使用 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.
默认情况下,您的必填字段验证器已启用(如果没有,则启用它)。因此,仅使用那些需要禁用它的条件,然后就完成了。
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.