简体   繁体   English

使表单字段在编辑时保持隐藏-Javascript和Ruby on Rails

[英]Keep form fields hidden on edit - Javascript and Ruby on Rails

I have a form in rails (3.2) where I hide some fields depending on the update value of some comboboxes. 我在rails(3.2)中有一个表单,其中根据某些组合框的更新值隐藏了一些字段。

For this, I'm currently using javascript, here's an example: 为此,我当前正在使用javascript,这是一个示例:

$('#sample_pcris').change(function(){
        if( $(this).val() === "Nao" ) {
                $(".resultpcr").hide();
            } else {
                $(".resultpcr").show();   
           } 
        return false;
    });

This works well when I'm creating the record, but I want to make this "persistent" and "client independent", that is, imagine I create the record and some of the fields are hidden, if someone else tries to edit that record later I want the same fields to remain hidden unless he changes the value of the combobox... 当我创建记录时,这很好用,但是我想使它“持久”并且“与客户端无关”,也就是说,假设我创建了记录,并且如果其他人试图编辑该记录,则某些字段将被隐藏后来我希望这些字段保持隐藏状态,除非他更改了组合框的值...

What's the best approach for this? 最好的方法是什么? Serialize in javascript? 用JavaScript序列化? Or do you suggest something else? 还是您有其他建议?

All ideas are welcome :) Thank you 欢迎所有想法:)谢谢

There are (at least) two ways to do this. 有(至少)两种方法。 Either you use JavaScript to hide the field after the page has loaded or you hide it right in the template. 您可以在页面加载后使用JavaScript隐藏该字段,也可以将其直接隐藏在模板中。

The first approach would look something like: 第一种方法如下所示:

function handleHiding() {
    if( $('#sample_pcris').val() === "Nao" ) {
        $(".resultpcr").hide();
    } else {
        $(".resultpcr").show();   
    } 
}

$(document).ready(function() {
    handleHiding();

    $('#sample_pcris').change(function(){
        handleHiding();
    });
});

Second would look like (I am using pretty much random code, but it should give you the idea): 第二个看起来像(我正在使用很多随机代码,但是应该可以给你一个主意):

<% if @sample_pcris == 'Nao' %>
    <%= f.text_field :resultpcr, class: 'resultpcr', style: 'display: none' %>
<% else %>
    <%= f.text_field :resultpcr, class: 'resultpcr' %>
<% end %>

Untested, obviously. 显然,未经测试。

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

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