如何让jquery独立
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确,您有一个
validate()
函数(您没有显示),它验证指定的项目并在 OK 或false< 时返回
true
/code> 如果无效。提交时,您希望对多个项目调用validate()
,如果其中任何返回false
,则返回false
提交(相反,仅当所有验证都为true
时才返回true
。现有代码设置
retVal
的方式>,一旦变为true
就会保持true
因为validate()
与true
或的任何布尔结果都将是true
尝试这样做:首先默认返回
true
,然后如果validate(val)
返回将其更改为
使用false
。 >false&&
代替。||
则意味着一旦retVal
为false
,它将保持false
在我看来,这样会更清楚。只需完全删除&&
并说:If I understand correctly, you have a
validate()
function (which you don't show) which validates the specified item and returnstrue
if OK orfalse
if invalid. On submit you want to callvalidate()
for several items, and if any of them returnfalse
returnfalse
to the submit (conversely, returntrue
only if all the validations aretrue
.The way your existing code sets
retVal
, once it becomestrue
it will staytrue
because any boolean result fromvalidate()
ORed withtrue
will betrue
. Try this instead:This starts with a default return of
true
, and then changes it tofalse
ifvalidate(val)
returnsfalse
. Using&&
instead of||
then means that onceretVal
isfalse
it will stayfalse
. In my opinion it would be clearer to just get rid of the&&
altogether and say: