简体   繁体   中英

How to I get access to the `previous` method for a link?

I am trying to follow the railscast for Nested Model Forms but I am running into a problem writing the Java Script . I have been working with Rails for a couple of weeks now but this is the first time I have encountered JavaScript , so forgive me if this sounds a little nubbish.

I am trying to remove elements from the form as the cast suggests but it is giving me the following error. (I am using Chrome's debugging tools to view the errors)

Uncaught TypeError: Object [object Object] has no method 'previous' application.js?body=1:10222
remove_class_row application.js?body=1:10222
onclick read:95

This is my view, /app_root/app/views/classifications/read.html.erb:

<table id="classification_read_table">
    <tr ><td id="classification_read_table_td">
        <b>Classification</b>
    </td></tr>
    <%= form_tag read_build_path  %>
        <% @classes.each do |f| %>
            <div class="class_row">
                <tr >
                    <td class="classification_read_table_td">
                        <%= fields_for "class_is[]", f do |g| %>
                        <%= g.label :name %>
                        <%= g.text_field :name %>
                        <%= g.hidden_field :_destroy%>
                        <%= link_to_function "remove", "remove_class_row(this)" %>
                    </td>
                </tr>
            </div>
        <% end %>
    <% end %>
</table>

This is my JS file app_root/app/assets/javascript/application.js:

function remove_class_row(link) {
    $(link).previous("input[type=hidden]").value = "1"; //Error on this line: 
    $(link).up(".class_row").hide();
}

I'm doing the javascript just as the rails cast shows, so I don't understand what might be going wrong. My first thoughts are that what I'm trying to do my be deprecated? Or for some reason my HTML isn't being set up in the right way.

Hopefully someone can point out the mundane detail that I am missing.

Thanks!!

I think you might be looking for .prev() . Also, you should use .val() to set a field value with jQuery.

$(link).prev("input[type=hidden]").val("1");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM