简体   繁体   English

如何获得链接的“上一个”方法的访问权限?

[英]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 . 我正在尝试遵循嵌套模型表单的规范,但是在编写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. 我已经和Rails一起工作了几周,但这是我第一次遇到JavaScript ,所以如果听起来有点讨厌,请原谅我。

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) (我正在使用Chrome的调试工具来查看错误)

Uncaught TypeError: Object [object Object] has no method 'previous' application.js?body=1:10222 未捕获的TypeError:对象[object Object]没有方法'previous'application.js?body = 1:10222
remove_class_row application.js?body=1:10222 remove_class_row application.js?body = 1:10222
onclick read:95 onclick阅读:95

This is my view, /app_root/app/views/classifications/read.html.erb: 这是我的观点,/ 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: 这是我的JS文件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. 我正在使用Rails演员表所示的JavaScript,所以我不明白可能出了什么问题。 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. 或者由于某种原因,我的HTML设置不正确。

Hopefully someone can point out the mundane detail that I am missing. 希望有人能指出我所缺少的世俗细节。

Thanks!! 谢谢!!

I think you might be looking for .prev() . 我认为您可能正在寻找.prev() Also, you should use .val() to set a field value with jQuery. 另外,您应该使用.val()通过jQuery设置字段值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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