使用jquery进行用户可用性验证

时间:2014-04-03 12:06:34

标签: jquery ruby-on-rails

在我的可用表单中,我获得的用户可用时间和日期如下:

<%= form_for(:Availability, :html => { :id => "availability" }) do |f| %>
    <table align="center" width="80%">
          <tr>
              <td width="13%">

              </td>
              <td>
                <div style="color:black; font-weight: bold;">
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                  <%= f.check_box(:Monday,{:id => "Monday",:value => "Monday"}) %>
                    <%= label_tag "Monday" %>
                </div>
              </td>
              <td>
                <div id="MondayDiv">
                  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <%= f.select :mondayFrom,options_for_select([
                          ["1","1"],
                        ["2","2"],
                          ["3","3"],
                          ["4","4"],
                        ["5","5"],
                        ["6","6"],
                        ["7","7"],
                        ["8","8"],
                        ["9","9"],
                        ["10","10"],
                        ["11","11"],
                        ["12","12"],
                     ]), {},:id => "DDL_TimeFromMon" %>

                    <%= f.select :ddl_MonFromAMPM ,options_for_select([["AM","AM"],["PM","PM"]]),{}, :id => "ddl_MonFromAMPM" %>

                    <%= label(:to, " to", {}, { :class => "labelTo" }) %>
                    <%= f.select :mondayTo,options_for_select([
                        ["1","1"],
                          ["2","2"],
                          ["3","3"],
                          ["4","4"],
                        ["5","5"],
                        ["6","6"],
                        ["7","7"],
                        ["8","8"],
                        ["9","9"],
                        ["10","10"],
                        ["11","11"],
                        ["12","12"],
                     ]), {},:id => "DDL_TimeToMon" %>

                     <%= f.select :ddl_MonToAMPM ,options_for_select([["AM","AM"],["PM","PM"]]), {}, :id => "ddl_MonToAMPM"  %>
                </div>
              </td>
          </tr>
          <tr>
    </table>

它工作正常,但问题是,如果用户检查星期一复选框并设置时间from 1 AM to 1 AM并且它使25小时和from 1 AM to 12 PM它24小时,所以我想使用jquery应用验证if用户输入from 1 AM to 1 AM并在jquery代码下方显示Time From Should be less then Time To我正在使用的错误消息:

    <script type="text/javascript">
$("#availability").submit(function ( event ) {

    var monday_from = $('#DDL_TimeFromMon').val();
    var monday_to = $('#DDL_TimeToMon').val();
    var hasError = false;

    if(monday_from > monday_to){
        alert("Time From Should be less then Time To")
        hasError = true;
    }

    if(hasError){
        event.preventDefault();
    }
});
</script>

但它没有向我显示任何错误信息,请帮我等你回复。 感谢。

1 个答案:

答案 0 :(得分:0)

从你的HTML看来,你似乎没有任何值为0的选项,但是在javascript中检查这个条件,返回false并且if块永远不会被执行。因此,您既不会看到任何错误,也不会得到预期的结果。