1
フィールドにjQuery経由でフォームを挿入するRailsアプリケーションがあります。これは、ページに1つのフォームがある場合に効果的ですが、2つのフォームがある場合、スクリプトはフィールドを正しいフォームに挿入しません。フォームIDを取得し、フィールドを適切なフォームに挿入するにはどうすればよいですか?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="marginBottom20"> <div class="row col-lg-8 padAll20 ghostBG locationDetails form"> <div class="col-lg-3 colMarginRight"> <label>First Name <em>*</em></label> <input class="wide" placeholder="First Name" type="text" name="courselocation[courseinstructors_attributes][70151989617000][first_name]" id="courselocation_courseinstructors_attributes_70151989617000_first_name" /> </div> <div class="col-lg-3 colMarginRight"> <label>Last Night <em>*</em></label> <input class="wide" placeholder="Last Name" type="text" name="courselocation[courseinstructors_attributes][70151989617000][last_name]" id="courselocation_courseinstructors_attributes_70151989617000_last_name" /> </div> <div class="col-lg-3 colMarginRight"> <label>Title <em>*</em></label> <input class="wide" placeholder="Current Title" type="text" name="courselocation[courseinstructors_attributes][70151989617000][title]" id="courselocation_courseinstructors_attributes_70151989617000_title" /> </div> <div class="col-lg-3"> <label>Company <em>*</em></label> <input class="wide" placeholder="Current Company" type="text" name="courselocation[courseinstructors_attributes][70151989617000][company]" id="courselocation_courseinstructors_attributes_70151989617000_company" /> </div> <div class="col-lg-12 padTop10"> <label>Profile Image <em>*</em></label> <div class="imageUpload"> <input placeholder="Image" class="wide" type="file" name="courselocation[courseinstructors_attributes][70151989617000][image]" id="courselocation_courseinstructors_attributes_70151989617000_image" /> </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="marginBottom20"> <div class="row col-lg-12 padAll20 ghostBG locationDetails form"> <div class="col-lg-3 colMarginRight"> <label>Name <em>*</em></label> <input class="wide" placeholder="Name of Location" type="text" name="course[courselocations_attributes][70152053053520][name]" id="course_courselocations_attributes_70152053053520_name" /> </div> <div class="col-lg-3 colMarginRight"> <label>Address <em>*</em></label> <input class="wide" placeholder="Address" type="text" name="course[courselocations_attributes][70152053053520][address]" id="course_courselocations_attributes_70152053053520_address" /> </div> <div class="col-lg-2 colMarginRight"> <label>City <em>*</em></label> <input class="wide" placeholder="City" type="text" name="course[courselocations_attributes][70152053053520][city]" id="course_courselocations_attributes_70152053053520_city" /> </div> <div class="col-lg-1 colMarginRight"> <label>State <em>*</em></label> <input class="wide" placeholder="XX" pattern="[A-Za-z]{2}" type="text" name="course[courselocations_attributes][70152053053520][state]" id="course_courselocations_attributes_70152053053520_state" /> </div> <div class="col-lg-2"> <label>Zipcode <em>*</em></label> <input class="wide" placeholder="12345" type="text" name="course[courselocations_attributes][70152053053520][zipcode]" id="course_courselocations_attributes_70152053053520_zipcode" /> </div> <div class="col-lg-12 padTop10"> <input type="hidden" value="false" name="course[courselocations_attributes][70152053053520][_destroy]" id="course_courselocations_attributes_70152053053520__destroy" /><a class="remove_fields" href="#">Remove</a> </div> </div></fieldset>" href="#">Add a new location</a>
</div>
</form>
第二の形式HTML
<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="marginBottom20"> <div class="row col-lg-8 padAll20 ghostBG locationDetails form"> <div class="col-lg-3 colMarginRight"> <label>First Name <em>*</em></label> <input class="wide" placeholder="First Name" type="text" name="courselocation[courseinstructors_attributes][70151989617000][first_name]" id="courselocation_courseinstructors_attributes_70151989617000_first_name" /> </div> <div class="col-lg-3 colMarginRight"> <label>Last Night <em>*</em></label> <input class="wide" placeholder="Last Name" type="text" name="courselocation[courseinstructors_attributes][70151989617000][last_name]" id="courselocation_courseinstructors_attributes_70151989617000_last_name" /> </div> <div class="col-lg-3 colMarginRight"> <label>Title <em>*</em></label> <input class="wide" placeholder="Current Title" type="text" name="courselocation[courseinstructors_attributes][70151989617000][title]" id="courselocation_courseinstructors_attributes_70151989617000_title" /> </div> <div class="col-lg-3"> <label>Company <em>*</em></label> <input class="wide" placeholder="Current Company" type="text" name="courselocation[courseinstructors_attributes][70151989617000][company]" id="courselocation_courseinstructors_attributes_70151989617000_company" /> </div> <div class="col-lg-12 padTop10"> <label>Profile Image <em>*</em></label> <div class="imageUpload"> <input placeholder="Image" class="wide" type="file" name="courselocation[courseinstructors_attributes][70151989617000][image]" id="courselocation_courseinstructors_attributes_70151989617000_image" /> </div> </div> </div></fieldset>" href="#">Add Another Instructor</a>
</div>
</form>
あなたは、フォームのHTMLコードを投稿することができますか? –
多分、生成されたhtmlの構造を含める必要があります。f12でページを検査し、そのコードをインクルードするようにしてください。 –
確かに両方の書式のHTMLを投稿してください –