如何在 select 字段中添加基于条件的 selected 状态
How to add condition based selected status in select field
Select 字段:
select#language_top onChange="window.location.href=this.value"
option value=("#{url_for(locale: 'en')}") data-image=("#{image_url('english.png')}") English
option value=("#{url_for(locale: 'de')}") data-image=("#{image_url('german.png')}") selected='' German
我想根据条件添加 selected=''
。我是这样做的:
select#language_top onChange="window.location.href=this.value"
- if params[:locale] == "en"
option value=("#{url_for(locale: 'en')}") data-image=("#{image_url('english.png')}") selected='' English
- else
option value=("#{url_for(locale: 'en')}") data-image=("#{image_url('english.png')}") English
- if params[:locale] == "de"
option value=("#{url_for(locale: 'de')}") data-image=("#{image_url('german.png')}") selected='' German
- else
option value=("#{url_for(locale: 'de')}") data-image=("#{image_url('german.png')}") German
我觉得可以重构。有什么想法吗?
这应该适合你:
select#language_top onChange="window.location.href=this.value"
option selected=(params[:locale] == "en") value=("#{url_for(locale: 'en')}") data-image=("#{image_url('english.png')}") English
option selected=(params[:locale] == "de") value=("#{url_for(locale: 'de')}") data-image=("#{image_url('german.png')}") German
Select 字段:
select#language_top onChange="window.location.href=this.value"
option value=("#{url_for(locale: 'en')}") data-image=("#{image_url('english.png')}") English
option value=("#{url_for(locale: 'de')}") data-image=("#{image_url('german.png')}") selected='' German
我想根据条件添加 selected=''
。我是这样做的:
select#language_top onChange="window.location.href=this.value"
- if params[:locale] == "en"
option value=("#{url_for(locale: 'en')}") data-image=("#{image_url('english.png')}") selected='' English
- else
option value=("#{url_for(locale: 'en')}") data-image=("#{image_url('english.png')}") English
- if params[:locale] == "de"
option value=("#{url_for(locale: 'de')}") data-image=("#{image_url('german.png')}") selected='' German
- else
option value=("#{url_for(locale: 'de')}") data-image=("#{image_url('german.png')}") German
我觉得可以重构。有什么想法吗?
这应该适合你:
select#language_top onChange="window.location.href=this.value"
option selected=(params[:locale] == "en") value=("#{url_for(locale: 'en')}") data-image=("#{image_url('english.png')}") English
option selected=(params[:locale] == "de") value=("#{url_for(locale: 'de')}") data-image=("#{image_url('german.png')}") German