简体   繁体   English

jQuery required(dependency-expression)无法正常工作

[英]jQuery required( dependency-expression ) is not working properly

I am using jquery validation plugin. 我正在使用jquery 验证插件。 When I was using required( dependency-expression ) i noticed that required( dependency-expression ) are not working properly. 当我使用required(dependency-expression)时,我注意到required(dependency-expression)无法正常工作。 as mentioned that i tried for 正如我所说的那样

$("#flight_availability").validate({
    rules: { 
       Trip: { required: true },
       DepartDate: { required : "#OneTrip:checked" },
       Origin: { required:"#OriginId:blank" }
    },
});

In the above mentioned code the code DepartDate: { required : "#OneTrip:checked" }, works fine but Origin: { required:"#OriginId:blank" } does not work. 在上面提到的代码中,代码DepartDate: { required : "#OneTrip:checked" },工作正常但是Origin: { required:"#OriginId:blank" }不起作用。

Whats wrong with the code? 代码有什么问题? I used firebug. 我用过萤火虫。 It did not show any errors. 它没有显示任何错误。 even used validation debug option too, but :( 甚至也使用验证调试选项,但:(

As per OP's comments: 根据OP的评论:

"even if OriginId is empty(blank), it validates it as true" “即使OriginId为空(空白),它也会将其验证为真”

Yes, that is exactly how you programmed it. 是的,这正是你编程的方式。

Origin: {
    required: "#OriginId:blank"
}

The above code is saying that the Origin field is only required when the #OriginId field is left blank. 上面的代码说只有当#OriginId字段留空时才required Origin字段。 Using #OriginId:filled , this logic is saying that the Origin field must remain empty if the #OriginId is filled in. If that's not exactly correct, then you can use the :blank selector instead. 使用#OriginId:filled ,这个逻辑说如果#OriginId被填入, Origin字段必须保持为空。如果这不完全正确,那么你可以使用:blank选择器代替。

http://jsfiddle.net/xMAs5/ http://jsfiddle.net/xMAs5/

If you want the opposite behavior then use the :filled selector instead. 如果你想要相反的行为,那么使用:filled选择器代替。

Origin: {
    required: "#OriginId:filled"
}

http://jsfiddle.net/xMAs5/1/ http://jsfiddle.net/xMAs5/1/


Otherwise, here is a demo using a custom method called empty that works as per your comment: "So if OriginId is empty means Origin value (anything) is invalid." 否则,这是一个使用名为empty的自定义方法的演示,根据你的评论工作: “因此,如果OriginId为空,则意味着Origin值(任何东西)无效。”

$(document).ready(function () {

    $.validator.addMethod('empty', function (value, element) {
        return (value === '');
    }, "This field must remain empty!");

    $("#flight_availability").validate({
        rules: {
            Trip: {
                required: true
            },
            DepartDate: {
                required: "#OneTrip:checked"
            },
            Origin: {
                empty: {
                    depends: function (element) {
                        return $("#OriginId").is(":blank")
                    }
                }
            }
        }
    });

});

DEMO: http://jsfiddle.net/Ab8bT/ 演示: http//jsfiddle.net/Ab8bT/

它可能是jQuery puesdo选择器尝试使用:empty而不是:blank。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM