mercredi 26 janvier 2022

How do I make this form submit by hitting return instead of physically hitting the "+" button *question updated*

Ruby on rails: (really new to this)

Okay so I have this code for a form that I want to submit when I hit the return key.

I realized I have to add this code somewhere

reference: Rails 4: how to allow user to submit form with enter key, without submit button

$('idhere').keypress(function(e){
    if(e.which == 13){
         $(this).closest('form').submit();
     }
  });
<div class="input-group">
    <%= f.text_field :sample_name, class: 'form-control', hidden: true %>
    <%= f.collection_select :sample_name2, @sample, :name, :name, {include_blank: true}, {class: 'form-control selectpicker sa-sample-name', data: {'live-search' => 'true'}} %>
    <%= f.select :rank, 1..3, {}, {class: 'form-control selectpicker sa-rank', data: {'live-search' => 'true'}} %>
    <%= f.submit '   +   ', class: 'btn btn-outline-primary' %>
  </div>

So my first question is, do I add the id to f.select or f.submit? Only reason I ask is because in my head if the user is already clicked into f.select would they have to be able to click return from there?

And do I add the code to my application.js file or into the .erb file that I have the original code in? And if I have to add it to my application.js file is there a specific place that it needs to go?

also if I make my id:

id: 'sa_submit'

Would I just reference it as sa_submit or .sa_submit or :sa_submit or name-of-erb-file.sa_submit?

Aucun commentaire:

Enregistrer un commentaire