【问题标题】:Validate select2 field before processing my form [duplicate]在处理我的表单之前验证 select2 字段[重复]
【发布时间】:2018-01-17 19:51:46
【问题描述】:

我正在尝试验证一个小表单,其中我有 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;
      }
  });});

【问题讨论】:

  • 我不知道您的问题,但您的 php 代码似乎语法错误:echo ''; 必须是 echo '';
  • 我的问题是我无法验证我的 select2 字段

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


【解决方案1】:

是的...你的代码有问题,改变

value="',$id,'" to 
value="'.$id.'"

【讨论】:

  • 我的问题是我无法验证我的 select2 字段
猜你喜欢
  • 2011-06-22
  • 1970-01-01
  • 1970-01-01
  • 2013-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-13
相关资源
最近更新 更多