简体   繁体   English

如何以简单的形式存储输入值? 导轨

[英]How to store input values ​in simple for form? Rails

I have this simple for form:我有这个简单的形式:

<%= simple_form_for @user do |f| %>
  <%= f.input :username, label_html: { class: 'my_class' }, hint_html: { class: 'hint_class' } %>
  <%= f.input :age %>
  <%= f.button :submit %>
<% end %>

I would like to take the values that the user enters to use as conditionals我想将用户输入的值用作条件

Take a value of:age input or:username input.取值:年龄输入或:用户名输入。 Take these two values given by the user without the user giving the submit button取用户给出的这两个值,而不需要用户给出提交按钮

I don't know if there is any way that rails allows to do that, or if not use a simple jquery.我不知道rails是否允许这样做,或者如果不使用简单的jquery。

If you could help me I would be very happy.如果你能帮助我,我会很高兴。 Thank you very much非常感谢

You can use simple Jquery您可以使用简单Jquery

<%= simple_form_for @user do |f| %>
  <%= f.input :username, class: 'username' %>
  <small class="error_username"></small>
  <%= f.input :age, class: 'age' %>
  <%= f.button :submit %>
<% end %>
--------------------------------------------------
form.js
Jquery + Ajax
$(document).ready(function() {
  var timeout = null;
  $('.username').keyup(function() {
     clearTimeout(timeout);
     timeout = setTimeout(function () {       
        checkUserName($(this).val());
     }, 1000)
  });

  checkUserName(username) {
     $.ajax({
        url: "check_user_name",
        type: "post",
        data: {username: username},
        success: function(res) {
            // res = {message: 'Username is existed'}
            $(".error_username").html(res['message'])
        },
        error: function(err) {
           console.log(err)
        }
     })
  }
})




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

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