将复选框值发送到rails model字段

时间:2015-03-10 22:38:16

标签: javascript jquery ruby-on-rails

我想将我的复选框(true / false)的值发送到我的rails模型字段(:taxable),以便对该按钮进行状态更改。

到目前为止,我有这个,但我不知道从哪里开始

<script>
$(document).ready(function() {
  $('.taxable').on('change', function(event) {
    var checkbox = $(event.target);
    var isChecked = $(checkbox).is(':checked');
    /* */
  });
});
</script>


<%= f.input :taxable, :as => :boolean, hint: "Is order taxable?",:id => 'taxable', :class => "taxable", :remote => true %>

1 个答案:

答案 0 :(得分:0)

试试这个:

<script>
$(document).ready(function() {
  $('.taxable').on('change', function(event) {
    var checkbox = $(event.target);
    var isChecked = $(checkbox).is(':checked');

    $.ajax({
      type: "POST",
      url: "/url",
      data: { params: { taxable: isChecked } },
      success:(data) {
        alert(data)
        return true;
},
      error:(data) {
        return false
}
    })
    /* */
  });
});
</script>


<%= f.input :taxable, :as => :boolean, hint: "Is order taxable?",:id => 'taxable', :class => "taxable", :remote => true %>