如何让jquery独立

发布于 2025-01-03 01:07:03 字数 566 浏览 1 评论 0原文

我有一个 jquery 来验证我的下拉列表。如果单选框上选择“是”,我可以从下拉列表中选择。如果单选框上的“否”下拉列表呈灰色。

如果两个单选框均为“是”并且两个下拉列表中的值均为“请选择”并单击提交按钮,则会发生验证错误并且不会重定向到index.html。

如果一个单选框处于“是”&下拉菜单“请选择”,另一个位于“否”,下拉菜单中的值为“红色”,单击提交按钮时会出现验证错误并重定向到index.html。显然这违背了验证的目的。

我希望有人能提供帮助,因为它让我发疯。!

我已将其范围缩小到这个问题:

});      
       $("input:submit").click(function(){         
           var retVal = false;
           $.each([1, 2], function(i, val){
              retVal = (validate(val) || retVal);
           });
            return retVal;   
       });

I have a jquery to validate my drop down. if 'yes' on radio box i can select from dropdown. if 'no' on radio box dropdown is greyed out.

If a both radio boxes are 'yes' and value in both dropdowns are 'please select' and submit button is clicked, a validation error occurs and DOES NOT redirect to index.html.

If one radio box is at 'yes' & dropdown 'please select' and the other at 'no' with value 'red' from dropdown and submit button is clicked a validation error occors and redirect to index.html. Obviously this defeats the purpose of a validation.

I hope someone can help as its driving me mad.!

I have narrowed it down to this being the probem:

});      
       $("input:submit").click(function(){         
           var retVal = false;
           $.each([1, 2], function(i, val){
              retVal = (validate(val) || retVal);
           });
            return retVal;   
       });

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

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

发布评论

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

评论(1

苏佲洛 2025-01-10 01:07:03

如果我理解正确,您有一个 validate() 函数(您没有显示),它验证指定的项目并在 OK 或 false< 时返回 true /code> 如果无效。提交时,您希望对多个项目调用 validate(),如果其中任何返回 false,则返回 false提交(相反,仅当所有验证都为 true 时才返回 true

现有代码设置 retVal 的方式>,一旦变为 true 就会保持 true 因为 validate()true 或的任何布尔结果都将是 true 尝试这样做:

       $("input:submit").click(function(){         
           var retVal = true;
           $.each([1, 2], function(i, val){
              retVal = validate(val) && retVal;
           });
            return retVal;   
       });

首先默认返回 true,然后如果 validate(val) 返回 将其更改为 false。 >false 使用 && 代替。 || 则意味着一旦 retValfalse ,它将保持 false 在我看来,这样会更清楚。只需完全删除 && 并说:

if (!validate(val)) retVal = false;

If I understand correctly, you have a validate() function (which you don't show) which validates the specified item and returns true if OK or false if invalid. On submit you want to call validate() for several items, and if any of them return false return false to the submit (conversely, return true only if all the validations are true.

The way your existing code sets retVal, once it becomes true it will stay true because any boolean result from validate() ORed with true will be true. Try this instead:

       $("input:submit").click(function(){         
           var retVal = true;
           $.each([1, 2], function(i, val){
              retVal = validate(val) && retVal;
           });
            return retVal;   
       });

This starts with a default return of true, and then changes it to false if validate(val) returns false. Using && instead of || then means that once retVal is false it will stay false. In my opinion it would be clearer to just get rid of the && altogether and say:

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