在rails上的ruby中显示错误消息

时间:2014-04-03 05:51:37

标签: ruby-on-rails

我在radio buttons页面上有两个user_Type_mentor名称user_Type_menteesign in,您可以在下面看到:

<%= form_for(:session, url: sessions_path,:html => {:id => "signin"}) do |f| %>
    <table width="100%">
      <tr>
        <td align="center">
          <%= f.radio_button :user_Type, "Mentor", :id => "rb_mentor" %>
          <%= f.label(:user_Type, "Mentor") %>
          &nbsp;&nbsp;
          <%= f.radio_button :user_Type, "Mentee", :id => "rb_mentee" %>
          <%= f.label(:user_Type, "Mentee") %>
        </td>
        <td align="center">
        </td>
      </tr>
    </table>
            <table width="83%" >
              <tr>
                <td align="center">
                  <div id="loginbutton">
                    <%= f.submit "Sign in", :style => "color:White;background-color:Transparent;border-style:none;font-family:Arial;font-size:Medium;height:35px" %>
                  </div>
                </td>
              </tr>
            </table>
            <% end %>

以下是javascript

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

    var mentor = $('#rb_mentor').val();
    var mentee = $('#rb_mentee').val();
    var hasError = false;

    if((mentor == 0) || (mentee == 0)){
        alert("Please select atleast one box")
        hasError = true;
    }

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

如果用户在没有选择任何单选按钮的情况下点击Sign in按钮,那么我想在Please Select页面上显示错误消息sign in。 请建议我,等待你的回复。 感谢。

1 个答案:

答案 0 :(得分:1)

为您的登录表单和复选框分配一个ID,让我们说&#34;登录&#34;,&#34; check1&#34;和&#34; check2&#34;,然后在javascript中

$("#login").submit(function ( event ) {

    var mentor = $('#check1').val();
    var mentee = $('#check2').val();
    var hasError = false;

    if((mentor == 0) && (mentee == 0)){
        alert("Please select Mentor and Mentee.")
        hasError = true;
    }

    if(hasError){
        event.preventDefault();
    }
});