簡體   English   中英

如何防止表單提交或頁面刷新后取消選中復選框?

[英]How to prevent check box to un-check after form submit or page refresh?

我在使用Ruby on Rails 3.2時出現復選框問題。 復選框的值通常保存到數據庫中,但是當我刷新頁面或單擊表單上的“提交”按鈕時,復選框不會保持選中狀態,我也不知道我在這里缺少什么。 每次刷新或提交時,都不會選中它。 多謝您的協助。 復選框has_available_space是一個布爾型數據庫值。

數據庫(復選框值)

    create_table :user_location do |t|
      t.boolean :has_available_space
    end  

路線

    namespace :users do
       match 'update_location' => 'location#update', :via => [:post]
    end

模型使用者

    has_one :user_location

模型用戶位置

    belongs_to :user

控制者

    def update 
     @user = User.find(params[:id])
     if @user.user_location.nil?
         @user.user_location = UserLocation.new
     end
     @user.user_location.has_available_space = params[:checkbox_has_available_space]
     @user.save!
     @user.user_location.save!
     redirect_to '/users/location'
    end

視圖

    <form id = 'location_form' action="/users/update_location" method="post">
       <input type="hidden" name="id" value="<%= @user.id %>" />
       <input id = 'has_available_space' type="checkbox" name="checkbox_has_available_space" value = '<%= @user.user_location.has_available_space %>' >I have available space
    </form> 

您需要將checked屬性添加到您的復選框元素:

`<input type="checkbox" checked />`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM