提交时从rails表单中删除必需的属性?

时间:2015-12-29 00:04:02

标签: javascript jquery html ruby-on-rails ruby-on-rails-4

我有一个带有必需属性的单选按钮的rails表单。我需要" Check"按钮。但当有人点击"马克缺席"按钮,它将从所有这些单选按钮中删除此必需属性。这是表格的样子:

enter image description here

这是我的表格。它与rails代码混合在一起,所以它不会让你感到困惑。

<table class="listing table table-striped" style="margin-bottom:50px;">
                <thead>
                    <tr class="header">
                        <th><span style="color:#F5F5F5;"><%= checkins_counter %>.</span> <%= company.name %></th>
                        <th colspan="2">

                            <div style="float:right;">

                            <% if checkin.checked == true %>
                                <%= link_to "#",:class => "minus_toggle btn btn-link", :style => "display:none;" ,:id => "minus_#{company.id}" do %>
                                <span class="glyphicon glyphicon-minus" style="color:#fff"></span> &nbsp;
                                <% end %>   

                                <%= link_to "#",:class => "plus_toggle btn btn-link", :id => "plus_#{company.id}" do %>
                                    <span class="glyphicon glyphicon-plus" style="color:#fff"></span> &nbsp;
                                <% end %>   
                            <% else %>
                                <%= link_to "#",:class => "minus_toggle btn btn-link" ,:id => "minus_#{company.id}" do %>
                                <span class="glyphicon glyphicon-minus" style="color:#fff"></span> &nbsp;
                                <% end %>   

                                <%= link_to "#",:class => "plus_toggle btn btn-link", :style => "display:none;" ,:id => "plus_#{company.id}" do %>
                                    <span class="glyphicon glyphicon-plus" style="color:#fff"></span> &nbsp;
                                <% end %>   
                            <% end %>

                            <%= hidden_field_tag :week, @week %>
                            <%= hidden_field_tag :company_id, company.id %>
                            <%= hidden_field_tag :checkins_size, @checkins.size %>
                            <%= hidden_field_tag :previous_target_content, previous_target %>
                            <%= f.submit "Check", :class => "btn btn-success btn-sm" %> &nbsp;
                                    <% if checkin.absent == true  %>
                                        <span id="undo_btn_<%= company.id %>">
                                            <%= f.submit "Undo", :class => "btn btn-danger btn-sm" %>
                                            Marked as absent
                                        </span>

                                        <%= f.submit "Mark as absent", :class => "btn btn-danger btn-sm absent_btn", :id => "hihi", :style => "display:none;" %>    
                                    <% else %>
                                        <span id="absent_btn_<%= company.id %>" style="display:none;">
                                            <%= f.submit "Undo", :class => "btn btn-danger btn-sm" %>
                                            Marked as absent
                                        </span>

                                        <%= f.submit "Mark as absent", :class => "btn btn-danger btn-sm absent_btn", :id => "absent_btn_#{company.id}" %>   
                                    <% end %>
                            </div>
                        </th>
                    </tr>   
                </thead>
                <% if checkin.checked == true %>
                    <tbody id="tbody_<%= company.id %>" style="display:none;">
                <% else %>
                    <tbody id="tbody_<%= company.id %>">
                <% end %>
                    <tr>
                        <td>Target</td>
                        <td>
                            <b>Previous target: </b><br/>
                            <% if previous_target != "" && !previous_target.blank? %>   
                                <i><%= previous_target %></i>
                            <% else %>
                                N/A
                            <% end %>

                            <br/>
                            <% if @week != "1" %>
                                <% if checkin.previous_target == false && checkin.checked == false  %>
                                 Done  <%= f.radio_button :previous_target, 'true', :checked => 'false', :required => true %>
                                 Not Done  <%= f.radio_button :previous_target, 'false', :checked => 'false', :required => true %>
                                <% else %>
                                 Done  <%= f.radio_button :previous_target, 'true', :required => true %>
                                 Not Done  <%= f.radio_button :previous_target, 'false', :required => true %>
                                <% end %>
                                <br/>
                            <% end %>
                            <br/>

                            <b>New target: </b><br/>
                            <%= f.text_area :target, :class => "form-control", :id => "target_#{company.id}" %>
                            <br/>
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>Comment</td>
                        <td><%= f.text_area :comment, :class => "form-control" %></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>Points</td>
                        <td>
                            1 <%= f.radio_button :point1, '1', :required => true %> &nbsp;
                            2 <%= f.radio_button :point1, '2', :required => true %> &nbsp;
                            3 <%= f.radio_button :point1, '3', :required => true %> &nbsp;
                            4 <%= f.radio_button :point1, '4', :required => true %> &nbsp;
                            5 <%= f.radio_button :point1, '5', :required => true %> &nbsp;
                            Execution<br/><br/>

                            1 <%= f.radio_button :point2, '1', :required => true %> &nbsp;
                            2 <%= f.radio_button :point2, '2', :required => true %> &nbsp;
                            3 <%= f.radio_button :point2, '3', :required => true %> &nbsp;
                            4 <%= f.radio_button :point2, '4', :required => true %> &nbsp;
                            5 <%= f.radio_button :point2, '5', :required => true %> &nbsp;
                            Team<br/><br/>

                            1 <%= f.radio_button :point3, '1', :required => true %> &nbsp;
                            2 <%= f.radio_button :point3, '2', :required => true %> &nbsp;
                            3 <%= f.radio_button :point3, '3', :required => true %> &nbsp;
                            4 <%= f.radio_button :point3, '4', :required => true %> &nbsp;
                            5 <%= f.radio_button :point3, '5', :required => true %> &nbsp;
                            Initiative<br/><br/>

                            1 <%= f.radio_button :point4, '1', :required => true %> &nbsp;
                            2 <%= f.radio_button :point4, '2', :required => true %> &nbsp;
                            3 <%= f.radio_button :point4, '3', :required => true %> &nbsp;
                            4 <%= f.radio_button :point4, '4', :required => true %> &nbsp;
                            5 <%= f.radio_button :point4, '5', :required => true %> &nbsp;
                            Prompt communication <br/><br/>

                            1 <%= f.radio_button :point5, '1', :required => true %> &nbsp;
                            2 <%= f.radio_button :point5, '2', :required => true %> &nbsp;
                            3 <%= f.radio_button :point5, '3', :required => true %> &nbsp;
                            4 <%= f.radio_button :point5, '4', :required => true %> &nbsp;
                            5 <%= f.radio_button :point5, '5', :required => true %> &nbsp;
                            Coachability<br/>
                        </td>
                        <td></td>   
                    </tr>
                </tbody>
            </table>

以下是我尝试过的javascript:

$(".absent_btn").click(function()
{
    $(this).parent().parent().parent().parent().parent().find('tbody :input').each(function(){
        $(this).removeAttr('required');​​​​​
    });
});

p / s: 我需要升级,因为我在页面上有多个表单,就像您在上面的图片中看到的那样

3 个答案:

答案 0 :(得分:1)

代码是工作:

$(".absent_btn").click(function()
{
    $(this).parents("table").find("input[type=radio]").each(function(){
        $(this).removeAttr('required');​​​​​
    });
});

答案 1 :(得分:0)

你真的不需要升级。只需在tbody

上应用选择器即可
$(".absent_btn").click(function()
{
    $(this).closest('form').find('tbody input').each(function(){
        $(this).removeAttr('required');​​​​​
    });
});

切换:

$(".absent_btn").click(function()
{
    if($(this).is(":checked"))
    {
      $(this).closest('form').find('tbody input').each(function(){
          $(this).removeAttr('required');
      });
    }
    else
    {
      $(this).closest('form').find('tbody input').each(function(){
          $(this).attr('required','required');
      });
    }
});

答案 2 :(得分:0)

我不知道为什么.removeAttr不适合我。我试图检查它,浏览器给了我“Uncaught SyntaxError:Unexpected token ILLEGAL”错误,这根本没用。

无论如何,这是最终适合我的解决方案:

$(".absent_btn").click(function()
{
    $(this).parents('table').find('tbody input').prop('required', false);
});