사용자가 링크를 클릭 할 때 페이지에 동적으로 양식을 삽입하기 위해 Ruby on Rails에서 javascript를 실행 중입니다.왜 내 object.children이 정의되지 않습니까?
작동하지 않지만 한 가지 특정 버그가 염소를 가져옵니다. 즉, 내 양식은 form.children을 디버깅 콘솔에서 반환하지 않습니다. 여기
는 (레일로) 형태 :<div class="field">
<%= f.label :name, 'Tag:' %>
<%= f.text_field :name %>
</div>
application.js 그리고 여기에서 자바 스크립트이고;
function add_fields(link, association, form) {
console.log(form);
console.log(form.children);
link.insertBefore(form, link.previousSibling.previousSibling);
}
나는 앱이 텍스트 등을 출력하여 실제로 자바 스크립트를 호출하는지 여부를 테스트했으며 작동합니다. 문제는 내 양식에 분명합니다.
마지막으로, 콘솔 출력 : 그런데
<div class="field">
<label for="post_tags_attributes_4_name">Tag:</label>
<input id="post_tags_attributes_4_name" name="post[tags_attributes][4][name]" size="30" type="text" />
</div> application.js:21
undefined application.js:22
Uncaught Error: NOT_FOUND_ERR: DOM Exception 8
는 동적으로 삽입 중첩 된 형태에 railscast # 197를 구현하기 위해 노력하고 있으며 재해되었습니다!
--edit --- 여기
내가 필드 요소 빌드 add_fields 함수를 호출하는 방법입니다루비 기능 : 여기
module TagsHelper
def link_to_add_fields(name, post_form, association)
new_object = post_form.object.class.reflect_on_association(association).klass.new
tag_field = post_form.fields_for :tags, new_object do|tag_form|
render("tag_field", :f=>tag_form)
end
#debugger
link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(tag_field)}\")")
end
end
그리고이 tag_field의 HTML이입니다 :
<div class="field">
<%= f.label :name, 'Tag:' %>
<%= f.text_field :name %>
</div>
'form'과 (과) 같은 소리는 DOM 요소가 아닙니다. 'add_fields' 함수 (어떤 값들)를 어떻게 호출하는지 알려주시겠습니까? – Bergi
'children' 대신'childNodes'를 사용합니까? – alex
테스트 브라우저가 ['Element.children'] (https://developer.mozilla.org/en-US/docs/DOM/Element.children) – Musa