jQuery ajax调用从一个数字字段使用Rails不工作
jQuery ajax call from a numberfield using Rails is not working
对于我的应用程序,我有定期存款,我们可以在其中创建新的定期存款。现在,我想根据数字(365/730/1095/1460)更新利率字段。我输入了存款期限(number_field),我必须检查客户的年龄。
我已经在fixeddeposits_controller中计算了客户年龄。我不知道我错在哪里,too不工作。
Example 1:
1.1: If a customer age >58 && age<75, i want to open the fixed deposit for 365days means i have to sum the two fields rate(9.5%) + seniorincrement(0.5%) and then pass the value(10.0%) to rateofinterest field.
1.2: If a customer age >75, i want to open the fixed deposit for 365days means i have to sum the two fields rate(9.5%) + superseniorincrement(1.0%) and then pass the value(10.5%) to rateofinterest field.
1.3: If a customer age <58, i want to open the fixed deposit for 365days means i have to pass the rate(9.5%) field value alone to rateofinterest field.
其中as(rate, seniorincrement, superseniorincrement)字段来自利息表
为此,我使用AJAX/JQUERY,这是Mandeep在我之前的问题中建议的。
我已经实现了,但它不工作。我已经附上了我试过的代码。请检查一下,并给我一些建议。
_form.html.erb
<%= form_for @fixeddeposit do |f| %>
<% if @fixeddeposit.errors.any? %>
<h4>Couldn't open FD Account</h4>
<ul>
<% @fixeddeposit.errors.full_messages.each do |error| %>
<li><%= error %></li>
<% end %>
</ul>
<% end %>
<%= f.label :customer_name, class:'required' %>
<%= f.text_field :customername, :placeholder =>'Name' %>
<%= f.label :date_of_birth, class:'required' %>
<%= f.date_select :dateofbirth, { :include_blank => true, :start_year => 1900, :end_year => 2014 }, :id => "dateofbirth" %>
<%= f.label :Periods, class:'required' %>
<%= f.number_field :periods, :id => "fixeddeposit_periods", :placeholder => "Days", :class => "input-mini" %>
<%= f.label :Rate_Of_Interest %>
<%= f.text_field :rateofinterest, :id => "fixeddeposit_rateofinterest", :value => "", :disabled => true, :class => "input-medium" %>
<span class="help-block">auto-generated</span>
<div>
</div>
<%= f.submit "Open FD", class: "btn btn-primary" %>
<% end %>
</div>
</div>
application.js
$(document).on("change","#fixeddeposit_periods",function(){
var periods = $(this).val();
var dateofbirth = $("#dateofbirth").val();
$.ajax({
type: "POST",
url: "/rateofinterest",
data: { periods: periods, dateofbirth: dateofbirth }
});
});
fixeddeposits_controller
def calculate_age(dateofbirth)
@age = DateTime.now - dateofbirth/ 365
end
def calculate_rateofinterest
@periods = params[:periods]
@dateofbirth = params[:dateofbirth]
calculate_age(@dateofbirth)
if @age >= 58 && @age < 75
@rateofinterest = Rateofinterest.select('interestrates.id, interestrates.seniorincrement')
elsif @age >= 75
@rateofinterest = Rateofinterest.select('interestrates.id, interestrates.superseniorincrement')
else
@rateofinterest = Rateofinterest.select('interestrates.id, interestrates.rate')
end
respond_to do |format|
format.html{ redirect_to fixeddeposits_path }
format.js{}
format.json{}
end
end
calculate_rateofinterest.js.erb
$("#fixeddeposit_rateofinterest").val(@rate);
routes.rb
resources :fixeddeposits do
resources :interestrates
end
post "/rateofinterest" => "fixeddeposits#calculate_rateofinterest" , as: "calculate_rateofinterest"
我不知道为什么它不工作。
首先,在js代码中替换
var dateofbirth = $("#fixeddeposit_dateofbirth").val();
// you have mentioned your dob field's id as 'dateofbirth'
var dateofbirth = $("#dateofbirth").val();
在您的控制器中,您需要调用calculate_age
方法来获得@age
变量。替换
@dateofbirth = params[:dateofbirth]
@dateofbirth = params[:dateofbirth]
calculate_age(@dateofbirth)
我不确定,为什么你在你的calculate_age
方法定义中写了@age.save
。你可以把它取下来。
现在在calculate_rateofinterest.js.erb
文件中替换
$("#interestrates_rate").val(@rate);
$("#fixeddeposit_rateofinterest").val(@rate);
相关文章:
- 我如何在数字插入中使用逗号,这样它就不会'不要破坏我的输入字段
- 输入字段,只接受0到12之间的数字
- 如何限制一个字段只接受4个数字字符作为输入,并获得'It’这是潜水警报
- angularjs在数字(html5)字段中将0转换为1
- 简单的Javascript方式,在输入字段的每5位数字后添加一个空格
- AngularJs:在数字输入字段中设置验证的条件最小值和最大值
- 在粘贴事件Javascript的输入字段中删除所有非数字字符
- 如何使文本、数字和日期html输入字段以一致的方式支持Ctrl+Z(撤消)
- 如何从动态输入字段添加数字
- 要检查的正则表达式数值字段没有 /或具有所有相同的数字
- 如何在javascript中获取隐藏字段的数字和
- OData:对url请求中的数字(ID)字段进行通配符(startswith)筛选
- 根据输入字段中键入的数字更改td单元格的颜色
- 如何使用鼠标或触摸更改数字输入字段
- 将字符串返回到类型为“0”的输出字段;数字“;针对用户错误javascript
- 使用输入数字值作为另一个字段的输入数字最大值
- 我如何在文本输入字段中输入数字,而无需在 JavaScript 或 jQuery 中输入模式
- Angular2:仅包含数字的字段的自定义验证
- 从 JSON 解析数组,其中每个字段键都是一个数字
- 使用java脚本在acrobatpdf表单中更改字段数字样式