Lie*_*cin 0 jquery jquery-ui ruby-on-rails-3
我试图让一些模态对话框显示包含表单,而不进入实际视图.
一切正常,直到我想为其中一个字段添加datepicker.
在我的观点/计划/ _form我有:
<%= simple_form_for [@customer, @plan], :remote => true do |f| %>
<fieldset>
<%= f.input :price %>
<%= f.input :guests %>
<%= f.input :start_date, :as => :string, :input_html => { :class => 'date_picker' } %>
<%= f.input :end_date, :as => :string, :input_html => { :class => 'date_picker' } %>
<div class="form-actions">
<%= f.button :submit %>
</div>
</fieldset>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我从另一个观点来看:
<%= link_to 'New Plan', new_customer_plan_path(session[:customer_id]), :class => 'btn btn-primary', :remote => true, :id => 'create_plan_link' %>
Run Code Online (Sandbox Code Playgroud)
在我的application.js我有:
$(document).ready(function() {
$('#create_plan_link').click(function(e) {
var url = $(this).attr('href');
$('#modal').dialog({
title: "New Plan",
draggable: true,
resizable: false,
modal: true,
width:'auto',
open: function(event, ui){
return $(this).load(url + ' #content');
$("input.date_picker").datepicker();}
});
});
});
Run Code Online (Sandbox Code Playgroud)
现在,关于实际表单一切正常,但是在选择字段时它没有显示日期选择器.
当表单加载并进入控制台并键入:
$("input.date_picker").datepicker();
Run Code Online (Sandbox Code Playgroud)
然后它的作品!所以我错过了什么?
在您的代码中,您在调用创建datepicker的行之前返回(退出)该函数.像这样改变顺序:
open: function(event, ui){
$("input.date_picker").datepicker();
return $(this).load(url + ' #content');
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你!
编辑:在意识到您实际通过ajax加载表单后,调用该行时,您的datepicker元素将不会存在.您必须提供对jQuerys加载函数的回调,该加载函数在内容加载后执行,如下所示:
open: function(event, ui){
return $(this).load(url + ' #content', function() {
$("input.date_picker").datepicker();
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1879 次 |
| 最近记录: |