jQuery验证在表单中不起作用

时间:2017-07-05 07:27:56

标签: javascript jquery ruby-on-rails

我有一个表单,其中rails和jQuery两个验证都不起作用。有没有其他方法来进行验证,因为当我没有填写表单的任何字段并单击创建按钮记录时,在表中创建。

    <div class="box box-default">
      <div class="box-header with-border">
        <h3 class="box-title"> Employee Resignation </h3>
      </div><!-- /.box-header -->
      <div class="box-body">
        <%= bootstrap_form_for(@employee_resignation,html: {id: 'employee_resignation'}) do |f| %>
       <!--  <= f.hidden_field :employee_id, :value => current_user.employee_id %> -->
         <%= f.hidden_field :application_date, value: Date.today %>

          <div class="row">
            <div class="col-sm-3">
              <div class="form-group required">
                <%= f.select :employee_id, all_active_employee_with_code, {include_blank: "Select Employee",label: "Employee"},{:class=>"select2 select2-hidden-accessible",:style=>"width: 100%;",:tabindex=>"-1",onchange:"var a={id:$(this).val()}; $.get('/employee_resignations/display_notice_period',a,function(response){});"}%>    
              </div>
            </div>


            <div class="col-sm-3">
            <div class="form-group required">
                <div class="input-group">
                  <div class="input-group-addon">
                    <i class="fa fa-calendar"></i>
                  </div>
                  <%= f.text_field :resignation_date,class: 'resignation',label:'Resignation Date'%>
                </div>
              </div>
            </div>

            <div class="col-sm-3">
              <div class="field">
               <%= f.select :leaving_reason_id, all_leaving_reason,{label: 'Reason of Leaving',include_blank: " Select Reason of Leaving"} %>
              </div>
            </div>

            <div class="col-sm-3">
             <div class="field">
               <%= f.text_field :notice_period, label:'Notice Period (in days)',readonly: true %>
             </div>
            </div> 
          </div>  

        <div class="row">
            <div class="col-sm-12">
             <div class="field">
              <%= f.text_area :reason, label: "Reason" %>
            </div>
          </div>
        </div>

          <div class="row">

          <div class="col-sm-3">
           <div class="form-group required">
            <div class="input-group">
              <div class="input-group-addon">
                <i class="fa fa-calendar"></i>
              </div>
              <%= f.text_field :tentative_leaving_date, class: 'resignation',label:'Requested Releaving Date'%>
            </div>
           </div>
          </div>

          <div class="col-sm-3">
           <div class="field">
            <div class="input-group">
              <div class="input-group-addon">
                <i class="fa fa-calendar"></i>
              </div>
              <%= f.text_field :exit_interview_date, class: 'resignation',label:'Exit Interview Date'%>
            </div>
           </div>
          </div>

          <div class="col-sm-3">
           <div class="form-group required">
            <div class="input-group">
              <div class="input-group-addon">
                <i class="fa fa-calendar"></i>
              </div>
              <%= f.text_field :leaving_date, class: 'resignation',label:'Leaving Date'%>
            </div>
           </div>
          </div>

          <div class="col-sm-3">
            <label>Settled On</label>
            <div class="field">
              <div class="input-group">
                <div class="input-group-addon">
                  <i class="fa fa-calendar"></i>
                </div>
                <%= f.text_field :settled_on,hide_label: true, class: 'resignation'%>
              </div>
            </div>
          </div>

          </div>

          <div class="row">


          <div class="col-sm-12">
           <div class="field">
             <%= f.text_area :note, label:'Note' %>
           </div>
          </div>
         </div>

       <div class="row">
         <div class="col-sm-6">
          <div class="actions">
            <%= f.submit 'Create Employee Resignation',class: "btn btn-primary btn-sm"  %>
            <%= link_to 'Back', employee_resignations_path,class: 'btn btn-sm btn-default fa fa-arrow-left' %>
          </div>
         </div>
       </div>
      <% end %>

      </div>
    </div>

jQuery验证 -

<script>
$("#employee_resignation").validate({
    rules: {
      "employee_resignation[employee_id]":{
        required: true,
      },
      "employee_resignation[resignation_date]":{
        required: true,
      }
    },
    messages: {
      "employee_resignation[employee_id]":{
        required: "Please Select Employee",
      },
      "employee_resignation[resignation_date]":{
        required: "Please Select Resignation Date",
      } 
    },
    errorPlacement: function(error, element) {
     error.css({"color": "red", "font-size": "12px","font-weight" : "normal"})
     error.insertAfter(element.parent(element));
    }
   });
</script>

1 个答案:

答案 0 :(得分:0)

将此行添加到您的JavaScript文件:debugger;,然后打开您喜欢的浏览器的调试器,然后重新加载页面并单击表单。 您将暂停调试器,您可以使用控制台查看调用的方法,变量等,然后您可以逐步完成该过程。

调试绝对是您需要的技能,尤其是JavaScript,因此您可以花更多时间在其中,同时解决您的问题。