Rails表单选择验证

时间:2013-04-20 01:01:17

标签: javascript jquery html ruby-on-rails

我希望不接受我的Rails表单中的<option value="">Select one</option>

这是我的代码:

<div class="field">
    <p><%= f.label :category, "Category:" %></p>
    <%= f.select(:category, [["-- Select One --", "false"],'Analytics','Commerce','Content Management','Gaming','Green','Media','Social Media','Technology - Software','Technology - Hardware', 'Web']) %>
</div>

使用JQuery Validation插件:

$(document).ready(function(){
  $("input[name='commit']").click(function(){
    $("#new_startup").validate({
      rules: { startup[category]: { valueNotEquals: "false" } },
      messages: { startup[category]: { valueNotEquals: "Please select one" } } 
    });
    if ($("#new_startup").valid())
      location.href = "#";
      $("#tag").css("display","none");
      $("#thank-you").css("display","block");
  });
});

这不起作用......有更好的方法吗?

设置["-- Select One --", ""]也不起作用。

1 个答案:

答案 0 :(得分:2)

我相信您需要选择promptinclude_blank

<%= f.select(:category, ['Analytics','Commerce','Content Management','Gaming','Green','Media','Social Media','Technology - Software','Technology - Hardware', 'Web'], :include_blank => '-- Select One --') %>

有关详情,请参阅docs