在我的Rails应用程序.js中,我有两个java脚本,它们所做的是自动填充我的注册表单。第一个是基于放入emp_id text_field的内容。一旦放入内容,并且与我在另一个表中的内容相匹配,它就会提取用户的名字和person_id,并将它们放在相应的text_fields中。现在我有了第二个javascript,它会查看person_id text_field中的数据,因为从第一个javascript开始,它就有数据。但是,让第二个javascript工作的唯一方法是,如果我更改一个数字并点击tab,它就会填充我的manager_first_name text_field。。。有没有办法让第一个javascript提取初始数据,然后让第二个javascript自动对数据做出反应,我必须点击它或更改person_id text_field?
这是我的用户控制器,这些是我的javascipts中的方法。
类UserController<应用程序控制器
def populate_form
@visual = Visual.find_by_id(params[:emp_id])
@emp_first_name = @visual.first_name
@person_id = @visual.pds_alias
render :json => {
:emp_first_name => @emp_first_name,
:person_id => @person_id
}
end
def populate_manager
@manager = Manager.find_by_person_id(params[:person_id])
@mgr_first_name = @manager.mgr_name
render :json => {
:mgr_first_name => @mgr_first_name
}
end
end
这是我的看法
<div class='row form-group'>
<div class='col-xs-8 col-xs-offset-2 col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4 col-lg-2 col-lg-offset-5 text-right'>
<%= f.text_field :emp_id, tabindex: 1, id: 'emp_id', autofocus: true, placeholder: t( 'login_label' ), class: 'form-control' %>
</div>
</div>
<div class='row form-group'>
<div class='col-xs-8 col-xs-offset-2 col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4 col-lg-2 col-lg-offset-5 text-right'>
<%= f.text_field :emp_first_name, tabindex: 1, id: 'emp_first_name', autofocus: true, placeholder: t( 'login_label' ), class: 'form-control' %>
</div>
</div>
<div class='row form-group'>
<div class='col-xs-8 col-xs-offset-2 col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4 col-lg-2 col-lg-offset-5 text-right'>
<%= f.text_field :person_id, tabindex: 1, id: 'person_id', autofocus: true, placeholder: t( 'login_label' ), class: 'form-control' %>
</div>
</div>
<div class='row form-group'>
<div class='col-xs-8 col-xs-offset-2 col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4 col-lg-2 col-lg-offset-5 text-right'>
<%= f.text_field :mgr_first_name, tabindex: 1, id: 'mgr_first_name', autofocus: true, placeholder: t( 'login_label' ), class: 'form-control' %>
</div>
</div>
这是我的App.js
$(document).ready(function(){
$('#emp_id').change(function() {
var url = "/user/populate_form?emp_id="+$(this).val();
$.getJSON(url, function(data) {
if(!(data.emp_first_name === undefined))
$('#emp_first_name').val(data.emp_first_name);
if(!(data.person_id === undefined))
$('#person_id').val(data.person_id);
});
}
);
});
$(document).ready(function(){
$('#person_id').click(function() {
var url = "/user/populate_manager?person_id="+$(this).val();
$.getJSON(url, function(data) {
if(!(data.mgr_first_name === undefined))
$('#mgr_first_name').val(data.mgr_first_name);
});
}
);
});
这个怎么样(假设你有数据作为对象而不是数组,即使你有,你也可以使用Enumerable),你不必为此使用Javascript Ajax,
def populate_form
@visual = Visual.find_by_id(params[:emp_id])
@emp_first_name = @visual.first_name
@person_id = @visual.pds_alias
@manager_name = Manager.find_by_person_id(@person_id).mgr_name
render :json => {
:emp_first_name => @emp_first_name,
:person_id => @person_id,
:mgr_first_name => @manager_name
}
end