簡體   English   中英

如何以簡單的形式存儲輸入值? 導軌

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

我有這個簡單的形式:

<%= 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 %>

我想將用戶輸入的值用作條件

取值:年齡輸入或:用戶名輸入。 取用戶給出的這兩個值,而不需要用戶給出提交按鈕

我不知道rails是否允許這樣做,或者如果不使用簡單的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