I want to hide the button after pressing the Add Detail button and the number of lines exceeds a certain number of lines.

Asked 1 years ago, Updated 1 years ago, 40 views

We are currently producing the Rails app.
In that app, I press the Add Detail button to add a table line, but how do I hide the Add Detail button when the number of lines exceeds 5?

In the code below, you can still add more lines with the "Add Line" button still displayed after more than 5 lines.There is no problem with HTML and jQuery (JavaScript) samples instead of the following Rails code, so please advise.Thank you for your cooperation.

$(function(){
   function check_content(){
      if($('#costs_form_area.nested-fields') .length==5){
           $('.specification_btna.add_fields') .hide();
      } else{
          $('.specification_btna.add_fields').show();
      }
   }
});

<divid="costs_form_area">
  <%=f.fields_for: costs do | costs_form | %>
    <%=render 'cost_fields', f:costs_form%>
  <%end%>
  <div class="text-center specification_btn">
    <%=link_to_add_association 'Add line', f, :costs,class: 'add_fields'%>
  </div>
</div>

javascript ruby-on-rails jquery

2022-09-30 18:56

1 Answers

$(function(){
   function check_content(){
      if($('#costs_form_area.nested-fields') .length==5){
           $('.specification_btna.add_fields') .hide();
      } else{
          $('.specification_btna.add_fields').show();
      }
   }
});

Then you just defined a function in an unknown function, so
To Run

$(function(){
   ( function check_content(){
      if($('#costs_form_area.nested-fields') .length==5){
           $('.specification_btna.add_fields') .hide();
      } else{
          $('.specification_btna.add_fields').show();
      }
   })();
});

or

$(function(){
    if($('#costs_form_area.nested-fields') .length==5){
        $('.specification_btna.add_fields') .hide();
    } else{
        $('.specification_btna.add_fields').show();
    }
});

must be as shown in .


2022-09-30 18:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.