如何使用 select 列表验证输入字段?

How to validate input field with select list?

我需要一些帮助 jQuery 验证插件输入字段 "driveamount" 在 select 一个 isdrive 下拉列表后有一个值。

如果我 select "yes" 必须在输入字段 "driveamout" 中输入金额。 如果我 select "no" 输入字段 "driveamout" 不需要。

这是表格的一部分:

     <div class="row">

        <section class="col col-6">
            <label class="input">Amount
                <input type="text" name="driveamount" id="driveamount" value=" ">
            </label>
        </section>

        <section class="col col-6">
            <label class="select">Is Drive 
                <select name="isdrive " id="isdrive " onChange="Choice();">
                    <option value="">Select</option>
                    <option value="yes">Yes</option>
                    <option value="no">No</option>
                </select>

            </label>
        </section>                                          
    </div>

    $.validator.addMethod('driveaway', function (value, el, param) {
            if($('#isdrive').val() == "yes")
               return false;
            },"Please provide enter amount"); // Message added

 $("#addcar").validate({
          rules: {

            driveamount: {
              driveaway : true,
            },


          },
          messages: {
            }
          }
        });

试试这个

$(document).ready(function(){
$("#isdrive").change(function() {
    var selectedCountry = $(this). children("option:selected"). val();  
    if(selectedCountry == "yes"){
        $("#driveamount").prop('required',true);
    }
    else if(selectedCountry == "no"){           
        $("#driveamount").removeAttr("required");
    }
}); });

这是你的 html

    <div class="row">
    <section class="col col-6">
        <label class="input">Amount
            <input type="text" name="driveamount" id="driveamount" value=" ">
        </label>
    </section>

    <section class="col col-6">
        <label class="select">Is Drive 
            <select name="isdrive" id="isdrive">
                <option value="">Select</option>
                <option value="yes">Yes</option>
                <option value="no">No</option>
            </select>

        </label>
    </section>                                          
  </div>

// add the rule here
 $.validator.addMethod("valueNotEquals", function(value, element, arg){
  return arg !== value;
 }, "Value must not equal arg.");

 // configure your validation
 $("form").validate({
  rules: {
   SelectName: { valueNotEquals: "default" }
  },
  messages: {
   SelectName: { valueNotEquals: "Please select an item!" }
  }  
 });

终于解决了。感谢大家

$.validator.addMethod('driveaway', function (value, el, param) {
            if($('#isdrive').val() == "yes" ){
              if($('#daa').val() != ""){
                return true;
              }
                return false;

            }else if($('#isdrive').val() == "no"){
                return true;
            }else {
              return true;
            }
            // if($('#isdrive').val() == " " )
            //    return true;
            // if($('#isdrive').val() == "yes" )
            //    return false;
            // if($('#isdrive').val() == "no")
            //    return true;
        },"Please provide enter correct amount"); // Message added

    $("#addcar").validate({
              rules: {

                driveamount: {
                  driveaway : true,
                },


              },
              messages: {
                }
              }
            });