在处理表单之前验证select2字段

时间:2017-08-09 19:12:23

标签: javascript php jquery jquery-validate jquery-select2

我试图验证一个小表单,其中我有3个字段,其中一个我使用php加载一个选择字段,所有这些工作正常,现在当尝试使用select2用于此字段时,jquery-validate似乎没有考虑到它,并搜索了解决方案,但没有找到适合我的代码js验证规则,我怎么能解决这个问题。

我已经在操作系统中回顾了几个与此相关的问题,试图使其适应我的代码,但它对我不起作用。我需要一个如何解决这个问题的想法

<form id="form-reservation" action="<?php echo base_url();?>reservation/save" method="POST">
  <div class="form-group">
    <label class="form-label">customer</label>
    <div class="form-group">
      <select class="select2" id="cus" name="cus">
        <option>Select customer</option>
        <?php
          foreach ($get as $id => $name)
            echo '<option  value="',$id,'">'.$name.'</option>';
        ?> 
      </select>
    </div>
  </div>
  <div class="form-group">
    <label class="form-label">Monthly amount</label>
    <div class="input-group">
      <input type="number" class="form-control" id="monthly" name="monthly" placeholder="0.00">
      <div class="input-group-addon">$</div>
    </div>
  </div>
  <div class="form-group">
    <label class="form-label">total amount</label>
    <div class="input-group">
      <input type="number" class="form-control" id="total" name="total" placeholder="0.00">
      <div class="input-group-addon">$</div>
    </div>
  </div>
  <div class="form-actions">
    <button type="submit" class="btn btn-squared btn-info margin-inline">submit</button>
  </div>
</form>

我的js

$('document').ready(function(){
  $(".select2").select2();

  $('#form-reservation').validate({
      rules: {
        cus:{
          required: true
        },
        monthly:{
          number: true,
          required: true
        },
        total:{
          number: true,
          required: true
        }
      },
      highlight: function (input) {
        $(input).parents('.form-group').addClass('has-danger');
      },
      unhighlight: function (input) {
        $(input).parents('.form-group').removeClass('has-danger');
      },
      errorPlacement: function (error, element) {
        $(element).parents('.form-group').append(error);
      },
      submitHandler : function(_form){
        $.ajax({
          url: form.attr('action'),
          type: form.attr('method'),
          data: form.serialize(),
          dataType: 'json',
          success:function(response){
          }
        })
       return false;
      }
  });});

1 个答案:

答案 0 :(得分:-1)

是的......这是你代码中的一个问题,改变

value="',$id,'" to 
value="'.$id.'"
相关问题