简体   繁体   English

如何使用jQuery验证3个选择元素?

[英]How can I validate 3 select elements with jQuery?

I'm trying to validate 3 select fields on my form, my application is a score keeping application for hockey, and I am recording scoring plays. 我正在尝试验证表单上的3个选择字段,我的应用程序是曲棍球的得分保持应用程序,并且我正在记录得分比赛。 A is the goal scorer, B is the primary assist, and C is the secondary assist. A是进球手,B是主要助攻,C是次要助攻。 I have the following validation rules: 我有以下验证规则:

  1. A must not be empty 一个不能为空
  2. B cannot equal A or C B不能等于A或C
  3. C cannot equal A or B C不能等于A或B
  4. If B is empty, C must be empty 如果B为空,则C必须为空

So in other words, A, AB, or ABC are valid combinations, but all must be unique. 因此,换句话说,A,AB或ABC是有效的组合,但所有组合必须唯一。 I am disabling the submit button by applying the disabled bootstrap class/html attribute to the submit button. 我通过将禁用的引导类/ html属性应用于提交按钮来禁用提交按钮。

In my actual code, A = awayGoal, B = awayPAssist, C = awaySAssist 在我的实际代码中,A =离开目标,B =离开PAssist,C =离开SAssist

I currently have the following jQuery code: 我目前有以下jQuery代码:

var awayGoal = $("#awayGoal");
var awayPAssist = $("#awayPAssist");
var awaySAssist = $("#awaySAssist");
var awayGSubmit = $("#submitAwayScore");

$('#awayScore select').change(function() {
if(awayGoal.val() != "" && awayPAssist.val() != awayGoal.val() && awayGoal.val() != awaySAssist.val()) {
    if(awayPAssist.val() != "" && awaySAssist.val() != "") {
        awayGSubmit.disableButton();
    }
    awayGSubmit.clearDisabled();
}
else {
    awayGSubmit.disableButton();
}
});

This allows unique A + C combinations to pass through, however, A + C that are the same do not pass through. 这允许唯一的A + C组合通过,但是相同的A + C不会通过。 Each select element has the numbers of the players involved in the game loaded in to them. 每个选择元素都有加载到游戏中的玩家人数。 There is also the first option which is a static option, called N/A, the code for that is: <option value="">N/A</option> this allows the admin to have empty primary and secondary assists if they do not exist. 还有第一个option是一个静态选项,称为N / A,其代码为: <option value="">N/A</option>允许管理员在主和辅助助手为空的情况下使用它们不存在。

Any help would be greatly appreciated, and any way to make it more efficient is very welcome as well as I am just starting out with jQuery. 任何帮助将不胜感激,并且使它更有效的任何方式都非常受欢迎,而且我只是从jQuery开始。 Thanks in advance. 提前致谢。

Since you only have three cases and only three items you can brute force check for each condition and then OR them together to determine the button's disabled status. 由于只有三种情况,只有三种情况,您可以对每种情况进行强力检查,然后将它们进行或运算以确定按钮的禁用状态。

JSBin example JSBin示例

var awayGoal = $("#awayGoal");
var awayPAssist = $("#awayPAssist");
var awaySAssist = $("#awaySAssist");
var awayGSubmit = $("#submitAwayScore");


$('#awayScore select').change(function() {
  var A = awayGoal.val();
  var B = awayPAssist.val();
  var C = awaySAssist.val();
  console.log(A,B,C);

  if(// case 1: only A 
     (A !== "" && B === "" && C === "") ||  

     // case 2: only A and B, A != B
     (A !== "" && B !== "" && C === "" && A !== B) || 

     // case 3: A, B, C, all unique
     (A !== "" && B !== "" && C !== "" && A !== B && A !== C && B !== C) ) { 

  //awayGSubmit.clearDisabled();
  console.log("enable button");
}
else {
    //awayGSubmit.disableButton();
    console.log("disable button");
}
});

Im currently develop the system that have so many rule for validation. 我目前正在开发具有如此多验证规则的系统。 I try validate using jquery that I build on my own, but so many thing I have to think and a lot of code to write. 我尝试使用我自己构建的jquery进行验证,但是有很多事情我必须思考,需要编写很多代码。

I suggest you to try using this jquery validation engine . 我建议您尝试使用此jquery验证引擎 You can check The Demo page here It have so many features and the most interesting part is you can define your own rule or validation. 您可以在此处查看“演示”页面。它具有许多功能,最有趣的部分是您可以定义自己的规则或验证。 Have a try. 试试。

Good luck. 祝好运。

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

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