调用rails函数onChange

时间:2017-06-28 07:45:40

标签: javascript jquery ruby-on-rails forms

在我的ruby on rails项目中,我有一个带有一组滑块的表单,如下所示:

<%= range_field(:subproject, :ans1, in: 0..100, id: 'slider1', :class => 'range range--light', :'data-init' => 'auto', :step => 1, :value => $project_value_1.to_i) %>

滑块的总值应始终等于100可以提交的形式。否则,将显示错误消息。所有这一切都由这个函数处理:

$sumOfProjectValues = $project_value_1.to_i + $project_value_2.to_i + $project_value_3.to_i + $project_value_4.to_i + $project_value_5.to_i

          if $sumOfProjectValues == 100
              message = 'Project can be saved'
              btnStyle = 'confirmation'
              hideDisabled = 'none'
            else
              message = 'Project quota must add up to 100% before saving'
              btnStyle = 'alert'
              hideEnabled = 'none'
            end

每当值发生变化时,我想调用此表单验证函数(类似于JS中的onChange方法)。 我该怎么做?

1 个答案:

答案 0 :(得分:0)

尝试代码:

$(".range").onChange(function(){
  var sumOfProjectValues = $project_value_1.to_i + $project_value_2.to_i +    $project_value_3.to_i + $project_value_4.to_i + $project_value_5.to_i

          if sumOfProjectValues == 100
              alert('Project can be saved')
          else
              alert('Project quota must add up to 100% before saving')
          end
})