简体   繁体   English

验证下拉框选择?

[英]Validation for dropdown boxes selection?

Here My requirement is once we select the SON or Father in one select box and if I select 'Mother-In-law' or 'Father-In_law' in another select boxes,I want one alert message like 'In-laws are not applicable'. 在这里我的要求是,一旦我们在一个选择框中选择SON或父亲,如果我在另一个选择框中选择“婆婆”或“父亲在法律”,我想要一条警告信息,例如“公婆不适用”。

Here is my code, 这是我的代码,

if (arr.indexOf(!(($(this).val() == 'Son' || $(this).val() == 'Father'))) > -1) {
    alert('In-laws are not applicable');
    return false;
}

Can any one help me please. 任何人都可以帮助我。 The jsFiddle is here . jsFiddle就在这里

-Thanks. -谢谢。

You can check each one of the selects looking for Son or Father after the user selects Father-in-law or Mother-in-law : 在用户选择Father-in-lawMother-in-law后,您可以查看寻找SonFather每一个selects

if($(this).val()=='Mother-in-law' || $(this).val()=='Father-in-law'){

    //for each of the Relation selects, we check if anyone is Son or Father              
    $('.classSelect').each(function(){
        var currentSelect = $(this).val();
        if( currentSelect == 'Son' ||  currentSelect == 'Father'){
            alert("In-laws are not applicable");  
        }
    });

}   

Living example: http://jsfiddle.net/4QUMG/10/ 生活示例: http//jsfiddle.net/4QUMG/10/

Update 更新

Living example using your arr array: http://jsfiddle.net/4QUMG/12/ 使用arr数组的实例: http//jsfiddle.net/4QUMG/12/

Also, I would save the value of $(this).val() inside a variable. 另外,我会将$(this).val()的值保存在变量中。

If I understand you, what you want is to see if "Son" or "Father" were already selected, and we're currently selecting an "in-law", to throw the error. 如果我了解你,你想要的是看看“儿子”或“父亲”是否已被选中,而我们目前正在选择一个“姻亲”,以抛出错误。

if (arr.indexOf('Son') > -1 || arr.indexOf('Father') > -1 && currentVal.indexOf('-in-law') > -1) {
        alert('In-laws are not applicable');
        return false;
    }

That code will do that. 该代码将这样做。 If you want it to work in reverse, (ie, we can't select in-law before son or father, either) we'd need to add this: 如果你想要它反向工作(也就是说,我们不能在儿子或父亲之前选择姻亲),我们需要添加:

if ((arr.indexOf('Father-in-law') > -1 || arr.indexOf('Mother-in-law') > -1) && (currentVal === 'Son' || currentVal === 'Father')) {
        alert('In-laws are not applicable');
        return false;
    }

Also: you could take $(this).val() out to a variable at the beginning, so you're not checking it ever time. 另外:您可以将$(this).val()输出到开头的变量,这样您就不会再检查它了。 So just something like var currentVal = $(this).val() , and then reference that instead. 所以就像var currentVal = $(this).val() ,然后引用它。

Here's a fiddle: Fiddle 这是一个小提琴: 小提琴

Here's another option. 这是另一种选择。 This code is less efficient than the other answers since it will always iterate. 此代码的效率低于其他答案,因为它总是会迭代。

jQuery has two varieties of $.each(array, function(index, value)... This works similar to your current loops. jQuery有两种$.each(array, function(index, value)...这类似于你当前的循环。

var fields = new Array();
$("#dataTable tr select[name^='dRelation']").each(function(i){
    fields.push(this.value);
 });
var rel = $(this).val();
$.each(fields, function(i,val) {
    if((val=='Son' || val=='Father') && (rel == 'Father-in-law' || rel == 'Mother-in-law'))                                 
        alert("yep");
 });

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

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