在嵌套表单中输入的onclick设置值

时间:2014-05-28 16:38:52

标签: javascript jquery ruby-on-rails nested-forms nested-attributes

我想点击删除时将is_deleted设置为true。我部分完成它但是当我点击删除它时,只需将所有嵌套部门设置为is_deleted即可。 这是我的父母表格:

    .h3 Process Step
    = f.fields_for :departments do |d|
      %div.fields
        = render 'department_fields', :f => d
    %div.fields
      = link_to_add_fields "Add Department", f, :departments

Here is my nested_form
%div.fields#department_fields
  = f.input :title
  = f.text_field :is_deleted, id: "is_deleted", class: "is_deleted"
  .remove_department remove

这是我的javascript:

:javascript
  $(document).on('click', '.remove_department', function () {
    $('.is_deleted').val('1');
  })

这是我生成的html:

<div class="col-sm-2 col-md-offset-1" style="outline: 1px solid black;">
                  <div class="h3">Process Step</div>
                  <div class="fields">
                    <div class="fields" id="department_fields">
                      <div class="form-group string required dealership_departments_title"><label class="string required control-label" for="dealership_departments_attributes_0_title"><abbr title="required">*</abbr> Title</label><input class="string required form-control" id="dealership_departments_attributes_0_title" name="dealership[departments_attributes][0][title]" type="text" value="test"></div>
                      <input class="is_deleted" id="is_deleted" name="dealership[departments_attributes][0][is_deleted]" type="text" value="1">
                      <div class="remove_department">remove</div>
                    </div>
                    <script>
                      $(document).on('click', '.remove_department', function () {
                        //e.stopPropagation();
                       //alert($('.is_deleted').val());
                        $('.is_deleted').val('1');
                        //$(this).parent().hide();
                      })
                    </script>
                  </div>
                  <input id="dealership_departments_attributes_0_id" name="dealership[departments_attributes][0][id]" type="hidden" value="7"><div class="fields">
                    <div class="fields" id="department_fields">
                      <div class="form-group string required dealership_departments_title"><label class="string required control-label" for="dealership_departments_attributes_1_title"><abbr title="required">*</abbr> Title</label><input class="string required form-control" id="dealership_departments_attributes_1_title" name="dealership[departments_attributes][1][title]" type="text" value="Check In"></div>
                      <input class="is_deleted" id="is_deleted" name="dealership[departments_attributes][1][is_deleted]" type="text">
                      <div class="remove_department">remove</div>
                    </div>
                    <script>
                      $(document).on('click', '.remove_department', function () {
                        //e.stopPropagation();
                       //alert($('.is_deleted').val());
                        $('.is_deleted').val('1');
                        //$(this).parent().hide();
                      })
                    </script>
                  </div>

我想要的是在点击&#34时将is_deleted的设定值设为true;删除&#34;与该字段相关

2 个答案:

答案 0 :(得分:2)

这:

:javascript
  $(document).on('click', '.remove_department', function () {
    // change this - > $('.is_deleted').val('1'); to ->
   $(this).siblings('.is_deleted').val('1');
  })

答案 1 :(得分:0)

我刚刚解决了我自己。

  $('.remove_department').on('click', function(event) {
    $(this).prev().val('1');
  })