gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:27:35 25 4
gpt4 key购买 nike

我试图在点击删除时将 is_deleted 设置为 true。我部分完成了它,但是当我单击删除时,它只是将所有嵌套部门设置为 is_deleted true。这是我的 parent 表格:

    .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>

我想要的是在单击与该字段相关的“删除”时将 is_deleted 的值设置为 true

最佳答案

这个:

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

关于javascript - 嵌套表单中输入的 onclick 设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23917320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com