这是表格:
|policies|
|id| |name| |date|
1 ABC 2013-01-01
2 DEF 2013-01-21
这是控制器:
class PolicyController < ApplicationController
def index
@policies = Policy.find(:all,:conditions=>['date BETWEEN ? AND ?',params[cam],params[:cam2] ])
end
end
以下是模型:
class Policy < ActiveRecord::Base
end
以下是观点:
<% CalendarDateSelect.format=(:hyphen_ampm )%>
<% calendar_date_select_style "silver" %>
<% translation_calendar %>
<% form_tag :controller=>"policy",:action=>"index" do %>
From: <%= calendar_date_select_tag "cam", params[:cam] %>
To: <%= calendar_date_select_tag "cam2",params[:cam2] %>
<%= submit_tag "Search", :name => nil %></p>
<% end %>
<% @policies.each |p| do %>
<%= p.date %>
<% end %>
在我的2个日历文本有值之前,如何阻止SEARCH按钮?
我试过了:
class Policy < ActiveRecord::Base
validates_presence_of :cam
end
请有人可以帮我,或者是javascript吗?
我将非常感谢您的帮助。
以下是朋友告诉我的事情:
named_scope :by_calendar_date,lambda { |cam1, cam2| } {
if cam1.nil? && cam2.nil?
scoped
elsif cam1.nil?
where("date < ?",cam2)
elsif cam2.nil?
where("date > ?", cam1)
else
where("date BETWEEN ? AND ?",cam1,cam2)
end
}
#this would perform the same functionality so both are not needed but just for edification
def self.by_calendar_date(cam1,cam2)
if cam1.nil? && cam2.nil?
scoped
elsif cam1.nil?
where("date < ?",cam2)
elsif cam2.nil?
where("date > ?", cam1)
else
where("date BETWEEN ? AND ?",cam1,cam2)
end
end
答案 0 :(得分:1)
由于这是一个搜索功能,您不需要在我看来验证您可以这样做
class Policy < ActiveRecord::Base
named_scope :by_calendar_date,lambda{|cam1,cam2|
if cam1.nil? && cam2.nil?
{:conditions => {}}
elsif cam1.nil?
{:conditions => ["date < ?",cam2]}
elsif cam2.nil?
{:conditions => ["date > ?", cam1]}
else
{:conditions => ["date >= ? AND date <= ?",cam1,cam2]}
end
}
end
用
打电话def index
@policies = Policy.by_calendar_date(params[:cam1],params[:cam2])
end
这样,用户可以单独设置param或者根本不设置
答案 1 :(得分:0)
看看这个小提琴...... http://jsfiddle.net/qKG5F/641/
// note the change... I set the disabled property right away
<input type="submit" id="register" value="Register" disabled="disabled" />
(function() {
$('form > input').keyup(function() {
var empty = false;
$('form > input').each(function() {
if ($(this).val() == '') {
empty = true;
}
});
if (empty) {
$('#register').attr('disabled', 'disabled'); // updated according to http://stackoverflow.com/questions/7637790/how-to-remove-disabled-attribute-with-jquery-ie
} else {
$('#register').removeAttr('disabled'); // updated according to http://stackoverflow.com/questions/7637790/how-to-remove-disabled-attribute-with-jquery-ie
}
});
})()
关于这一点的好处是,你的表单中有多少输入字段并不重要,如果至少有1个为空,它将始终保持按钮被禁用。它还会检查.keyup()
上的空虚,我认为这对于可用性来说更方便。
我希望这会有所帮助。