开发者

How do I insert an input's value into a separate form?

I'm making a tool for my university that allows students to create a list of classes by doing the following:

  1. Searching by course title for their course via an autocomplete input field.

  2. Adding that course to a separate form that upon being submitted creates a course list.

I am trying to link the autocomplete and the course list form with an 'add to course list' button that inserts a hidden input fiel开发者_StackOverflow社区d into the course list form which can subsequently be submitted by a 'create course list' button.

My question is this: How do I take the value of the autocomplete input and insert it into the course list form without using AJAX?

So far I have something like the following:

<%= text_field_with_auto_complete :course, :title, :size => 40 %>
<%= link_to_function "Add to Course List" do |page|
    page.insert_html :top, :course_list, hidden_field(:courses, :course, 
    {:value => "$('course_title').value"}) %>

<% form_for(@course_list) do |f|%>
    <div id="course_list">Insert selected courses here.</div>
<% end %>


If you use the below code, you can add the course to the form upon selecting the course from auto-complete

   <%= text_field_with_auto_complete :course, :title, 
        {:url => '/courses/list', :method => 'get',
          :with => "'search='+element.value",
          :after_update_element =>
             "function (ele, value){
               #Here write your JavaScript code to for adding courselist to your form.
              }
        "} %>

In autocomplete you can run the JavaScript upon clicking on an item, I have written a blog about it but in my case I called an ajax function to update a form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜