Javascript jQuery-如何获取父表单ID(当有多个表单时)并将字段插入到正确的表单中

Javascript jQuery-如何获取父表单ID(当有多个表单时)并将字段插入到正确的表单中,javascript,jquery,ruby-on-rails,Javascript,Jquery,Ruby On Rails,我有一个Rails应用程序,它通过jQuery将字段插入表单。当页面上有一个表单时,这非常有效,但当有两个表单时,脚本不会将字段插入正确的表单中。如何获取表单ID并将字段插入适当的表单 $(function() { $('form').on('click', '.remove_fields', function(event) { $(this).prev('input[type=hidden]').val('1'); $(this).closest('fieldset').h

我有一个Rails应用程序,它通过jQuery将字段插入表单。当页面上有一个表单时,这非常有效,但当有两个表单时,脚本不会将字段插入正确的表单中。如何获取表单ID并将字段插入适当的表单

$(function() {
  $('form').on('click', '.remove_fields', function(event) {
    $(this).prev('input[type=hidden]').val('1');
    $(this).closest('fieldset').hide();
    return event.preventDefault();
  });
  return $('form').on('click', '.add_fields', function(event) {
    var regexp, time;
    time = new Date().getTime();
    regexp = new RegExp($(this).data('id'), 'g');
    $(this).before($(this).data('fields').replace(regexp, time));
    return event.preventDefault();
  });
});
供参考;以下是触发插入字段的链接:

<a class="add_fields" data-id="70151989617000" data-fields="<fieldset class=&quot;marginBottom20&quot;>  <div class=&quot;row col-lg-8 padAll20 ghostBG locationDetails form&quot;>    <div class=&quot;col-lg-3 colMarginRight&quot;>      <label>First Name <em>*</em></label>      <input class=&quot;wide&quot; placeholder=&quot;First Name&quot; type=&quot;text&quot; name=&quot;courselocation[courseinstructors_attributes][70151989617000][first_name]&quot; id=&quot;courselocation_courseinstructors_attributes_70151989617000_first_name&quot; />    </div>    <div class=&quot;col-lg-3 colMarginRight&quot;>      <label>Last Night <em>*</em></label>      <input class=&quot;wide&quot; placeholder=&quot;Last Name&quot; type=&quot;text&quot; name=&quot;courselocation[courseinstructors_attributes][70151989617000][last_name]&quot; id=&quot;courselocation_courseinstructors_attributes_70151989617000_last_name&quot; />    </div>    <div class=&quot;col-lg-3 colMarginRight&quot;>      <label>Title <em>*</em></label>      <input class=&quot;wide&quot; placeholder=&quot;Current Title&quot; type=&quot;text&quot; name=&quot;courselocation[courseinstructors_attributes][70151989617000][title]&quot; id=&quot;courselocation_courseinstructors_attributes_70151989617000_title&quot; />    </div>    <div class=&quot;col-lg-3&quot;>      <label>Company <em>*</em></label>      <input class=&quot;wide&quot; placeholder=&quot;Current Company&quot; type=&quot;text&quot; name=&quot;courselocation[courseinstructors_attributes][70151989617000][company]&quot; id=&quot;courselocation_courseinstructors_attributes_70151989617000_company&quot; />    </div>    <div class=&quot;col-lg-12 padTop10&quot;>      <label>Profile Image <em>*</em></label>      <div class=&quot;imageUpload&quot;>        <input placeholder=&quot;Image&quot; class=&quot;wide&quot; type=&quot;file&quot; name=&quot;courselocation[courseinstructors_attributes][70151989617000][image]&quot; id=&quot;courselocation_courseinstructors_attributes_70151989617000_image&quot; />      </div>    </div>  </div></fieldset>" href="#">Add Another Instructor</a>

第一格式HTML

<form class="edit_course" id="edit_course_7" enctype="multipart/form-data" action="/admin/courses/become-a-web-developer" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓">
  <div class="marginTop40">
    <a class="add_fields" data-id="70152053053520" data-fields="<fieldset class=&quot;marginBottom20&quot;>  <div class=&quot;row col-lg-12 padAll20 ghostBG locationDetails form&quot;>    <div class=&quot;col-lg-3 colMarginRight&quot;>      <label>Name <em>*</em></label>      <input class=&quot;wide&quot; placeholder=&quot;Name of Location&quot; type=&quot;text&quot; name=&quot;course[courselocations_attributes][70152053053520][name]&quot; id=&quot;course_courselocations_attributes_70152053053520_name&quot; />    </div>    <div class=&quot;col-lg-3 colMarginRight&quot;>      <label>Address <em>*</em></label>      <input class=&quot;wide&quot; placeholder=&quot;Address&quot; type=&quot;text&quot; name=&quot;course[courselocations_attributes][70152053053520][address]&quot; id=&quot;course_courselocations_attributes_70152053053520_address&quot; />    </div>    <div class=&quot;col-lg-2 colMarginRight&quot;>      <label>City <em>*</em></label>      <input class=&quot;wide&quot; placeholder=&quot;City&quot; type=&quot;text&quot; name=&quot;course[courselocations_attributes][70152053053520][city]&quot; id=&quot;course_courselocations_attributes_70152053053520_city&quot; />    </div>    <div class=&quot;col-lg-1 colMarginRight&quot;>      <label>State <em>*</em></label>      <input class=&quot;wide&quot; placeholder=&quot;XX&quot; pattern=&quot;[A-Za-z]{2}&quot; type=&quot;text&quot; name=&quot;course[courselocations_attributes][70152053053520][state]&quot; id=&quot;course_courselocations_attributes_70152053053520_state&quot; />    </div>    <div class=&quot;col-lg-2&quot;>      <label>Zipcode <em>*</em></label>      <input class=&quot;wide&quot; placeholder=&quot;12345&quot; type=&quot;text&quot; name=&quot;course[courselocations_attributes][70152053053520][zipcode]&quot; id=&quot;course_courselocations_attributes_70152053053520_zipcode&quot; />    </div>    <div class=&quot;col-lg-12 padTop10&quot;>      <input type=&quot;hidden&quot; value=&quot;false&quot; name=&quot;course[courselocations_attributes][70152053053520][_destroy]&quot; id=&quot;course_courselocations_attributes_70152053053520__destroy&quot; /><a class=&quot;remove_fields&quot; href=&quot;#&quot;>Remove</a>    </div>  </div></fieldset>" href="#">Add a new location</a>
  </div>
</form>
<form class="edit_courselocation" id="edit_courselocation_55" enctype="multipart/form-data" action="/admin/courselocations/austin-wework-austin-tx" accept-charset="UTF-8" data-remote="true" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="_method" value="patch">     
  <div class="marginTop20 marginBottom40">
    <a class="add_fields" data-id="70151989617000" data-fields="<fieldset class=&quot;marginBottom20&quot;>  <div class=&quot;row col-lg-8 padAll20 ghostBG locationDetails form&quot;>    <div class=&quot;col-lg-3 colMarginRight&quot;>      <label>First Name <em>*</em></label>      <input class=&quot;wide&quot; placeholder=&quot;First Name&quot; type=&quot;text&quot; name=&quot;courselocation[courseinstructors_attributes][70151989617000][first_name]&quot; id=&quot;courselocation_courseinstructors_attributes_70151989617000_first_name&quot; />    </div>    <div class=&quot;col-lg-3 colMarginRight&quot;>      <label>Last Night <em>*</em></label>      <input class=&quot;wide&quot; placeholder=&quot;Last Name&quot; type=&quot;text&quot; name=&quot;courselocation[courseinstructors_attributes][70151989617000][last_name]&quot; id=&quot;courselocation_courseinstructors_attributes_70151989617000_last_name&quot; />    </div>    <div class=&quot;col-lg-3 colMarginRight&quot;>      <label>Title <em>*</em></label>      <input class=&quot;wide&quot; placeholder=&quot;Current Title&quot; type=&quot;text&quot; name=&quot;courselocation[courseinstructors_attributes][70151989617000][title]&quot; id=&quot;courselocation_courseinstructors_attributes_70151989617000_title&quot; />    </div>    <div class=&quot;col-lg-3&quot;>      <label>Company <em>*</em></label>      <input class=&quot;wide&quot; placeholder=&quot;Current Company&quot; type=&quot;text&quot; name=&quot;courselocation[courseinstructors_attributes][70151989617000][company]&quot; id=&quot;courselocation_courseinstructors_attributes_70151989617000_company&quot; />    </div>    <div class=&quot;col-lg-12 padTop10&quot;>      <label>Profile Image <em>*</em></label>      <div class=&quot;imageUpload&quot;>        <input placeholder=&quot;Image&quot; class=&quot;wide&quot; type=&quot;file&quot; name=&quot;courselocation[courseinstructors_attributes][70151989617000][image]&quot; id=&quot;courselocation_courseinstructors_attributes_70151989617000_image&quot; />      </div>    </div>  </div></fieldset>" href="#">Add Another Instructor</a>
  </div>
</form>

“href=”#“>添加新位置
第二格式HTML

<form class="edit_course" id="edit_course_7" enctype="multipart/form-data" action="/admin/courses/become-a-web-developer" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓">
  <div class="marginTop40">
    <a class="add_fields" data-id="70152053053520" data-fields="<fieldset class=&quot;marginBottom20&quot;>  <div class=&quot;row col-lg-12 padAll20 ghostBG locationDetails form&quot;>    <div class=&quot;col-lg-3 colMarginRight&quot;>      <label>Name <em>*</em></label>      <input class=&quot;wide&quot; placeholder=&quot;Name of Location&quot; type=&quot;text&quot; name=&quot;course[courselocations_attributes][70152053053520][name]&quot; id=&quot;course_courselocations_attributes_70152053053520_name&quot; />    </div>    <div class=&quot;col-lg-3 colMarginRight&quot;>      <label>Address <em>*</em></label>      <input class=&quot;wide&quot; placeholder=&quot;Address&quot; type=&quot;text&quot; name=&quot;course[courselocations_attributes][70152053053520][address]&quot; id=&quot;course_courselocations_attributes_70152053053520_address&quot; />    </div>    <div class=&quot;col-lg-2 colMarginRight&quot;>      <label>City <em>*</em></label>      <input class=&quot;wide&quot; placeholder=&quot;City&quot; type=&quot;text&quot; name=&quot;course[courselocations_attributes][70152053053520][city]&quot; id=&quot;course_courselocations_attributes_70152053053520_city&quot; />    </div>    <div class=&quot;col-lg-1 colMarginRight&quot;>      <label>State <em>*</em></label>      <input class=&quot;wide&quot; placeholder=&quot;XX&quot; pattern=&quot;[A-Za-z]{2}&quot; type=&quot;text&quot; name=&quot;course[courselocations_attributes][70152053053520][state]&quot; id=&quot;course_courselocations_attributes_70152053053520_state&quot; />    </div>    <div class=&quot;col-lg-2&quot;>      <label>Zipcode <em>*</em></label>      <input class=&quot;wide&quot; placeholder=&quot;12345&quot; type=&quot;text&quot; name=&quot;course[courselocations_attributes][70152053053520][zipcode]&quot; id=&quot;course_courselocations_attributes_70152053053520_zipcode&quot; />    </div>    <div class=&quot;col-lg-12 padTop10&quot;>      <input type=&quot;hidden&quot; value=&quot;false&quot; name=&quot;course[courselocations_attributes][70152053053520][_destroy]&quot; id=&quot;course_courselocations_attributes_70152053053520__destroy&quot; /><a class=&quot;remove_fields&quot; href=&quot;#&quot;>Remove</a>    </div>  </div></fieldset>" href="#">Add a new location</a>
  </div>
</form>
<form class="edit_courselocation" id="edit_courselocation_55" enctype="multipart/form-data" action="/admin/courselocations/austin-wework-austin-tx" accept-charset="UTF-8" data-remote="true" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="_method" value="patch">     
  <div class="marginTop20 marginBottom40">
    <a class="add_fields" data-id="70151989617000" data-fields="<fieldset class=&quot;marginBottom20&quot;>  <div class=&quot;row col-lg-8 padAll20 ghostBG locationDetails form&quot;>    <div class=&quot;col-lg-3 colMarginRight&quot;>      <label>First Name <em>*</em></label>      <input class=&quot;wide&quot; placeholder=&quot;First Name&quot; type=&quot;text&quot; name=&quot;courselocation[courseinstructors_attributes][70151989617000][first_name]&quot; id=&quot;courselocation_courseinstructors_attributes_70151989617000_first_name&quot; />    </div>    <div class=&quot;col-lg-3 colMarginRight&quot;>      <label>Last Night <em>*</em></label>      <input class=&quot;wide&quot; placeholder=&quot;Last Name&quot; type=&quot;text&quot; name=&quot;courselocation[courseinstructors_attributes][70151989617000][last_name]&quot; id=&quot;courselocation_courseinstructors_attributes_70151989617000_last_name&quot; />    </div>    <div class=&quot;col-lg-3 colMarginRight&quot;>      <label>Title <em>*</em></label>      <input class=&quot;wide&quot; placeholder=&quot;Current Title&quot; type=&quot;text&quot; name=&quot;courselocation[courseinstructors_attributes][70151989617000][title]&quot; id=&quot;courselocation_courseinstructors_attributes_70151989617000_title&quot; />    </div>    <div class=&quot;col-lg-3&quot;>      <label>Company <em>*</em></label>      <input class=&quot;wide&quot; placeholder=&quot;Current Company&quot; type=&quot;text&quot; name=&quot;courselocation[courseinstructors_attributes][70151989617000][company]&quot; id=&quot;courselocation_courseinstructors_attributes_70151989617000_company&quot; />    </div>    <div class=&quot;col-lg-12 padTop10&quot;>      <label>Profile Image <em>*</em></label>      <div class=&quot;imageUpload&quot;>        <input placeholder=&quot;Image&quot; class=&quot;wide&quot; type=&quot;file&quot; name=&quot;courselocation[courseinstructors_attributes][70151989617000][image]&quot; id=&quot;courselocation_courseinstructors_attributes_70151989617000_image&quot; />      </div>    </div>  </div></fieldset>" href="#">Add Another Instructor</a>
  </div>
</form>

经过一番尝试,我意识到在Rails中发出AJAX请求时,不会重新加载JS

function initialise(){
  $(function() {
    $('form').on('click', '.remove_fields', function(event) {
      $(this).prev('input[type=hidden]').val('1');
      $(this).closest('fieldset').hide();
      return event.preventDefault();
    });
    return $('form').on('click', '.add_fields', function(event) {
      var linkID = $(this).attr("data-id")
      alert(linkID)
      var regexp, time;
      time = new Date().getTime();
      regexp = new RegExp($(this).data('id'), 'g');
      $(this).before($(this).data('fields').replace(regexp, time));
      return event.preventDefault();
    });
  });
};
$(document).ready(function(){
    initialise();
});
然后在AJAX请求上调用以下命令以加载第二个表单:

$(document).ajaxComplete(function () {
    initialise();
});

在仔细研究了一下之后,我意识到在Rails中发出AJAX请求时,不会重新加载JS

function initialise(){
  $(function() {
    $('form').on('click', '.remove_fields', function(event) {
      $(this).prev('input[type=hidden]').val('1');
      $(this).closest('fieldset').hide();
      return event.preventDefault();
    });
    return $('form').on('click', '.add_fields', function(event) {
      var linkID = $(this).attr("data-id")
      alert(linkID)
      var regexp, time;
      time = new Date().getTime();
      regexp = new RegExp($(this).data('id'), 'g');
      $(this).before($(this).data('fields').replace(regexp, time));
      return event.preventDefault();
    });
  });
};
$(document).ready(function(){
    initialise();
});
然后在AJAX请求上调用以下命令以加载第二个表单:

$(document).ajaxComplete(function () {
    initialise();
});

你可以发布表单的HTML代码吗?也许你应该包括生成的HTML的结构,你只需使用f12检查页面并包含代码,这样我们就可以设计出最佳的解决方案,发布两个表单的HTML可以删除不相关的部分吗?这是很多代码。是的,对不起,我以为我复制了错误的版本n、 你可以发布表单的HTML代码吗?也许你应该包括生成的HTML的结构,你只需使用f12检查页面并包含代码,这样我们就可以设计出最佳解决方案,发布两个表单的HTML扫描你删除不相关的部分?这是很多代码。是的,对不起,我想我复制了错误的版本离子。