我有Rails应用程序,在这里我能够从选择国家:选择二提交值,而不是ID
= f.input :country, label: t("heading.country"), as: :string, input_html: { class: 'select2', data: { allowadd: true, depth: 0, id: @post.location.country.id, url: search_locations_path } }
选择二由连接到这个元素:
loadSelect2 = (selector = '.select2') ->
$(@).select2
ajax:
data: (term, page) ->
search: term
depth: $(@).data('depth')
results: (data, page) ->
results: data
url: $(@).data('url')
createSearchChoice: (term, data) ->
if $(data).filter(->
@title.localeCompare(term) is 0
).length is 0
id: term
title: term
createSearchChoicePosition: 'bottom'
formatResult: (item) ->
item.title
formatSelection: (item) ->
item.title
initSelection: (element, callback) ->
callback
id: $(element).data('id')
title: $(element).val()
所以,正如你看到的,我能够选择国家或添加一个(如果没有可用的国家)。另外我从data-id
里面加载从initSelection
输入的val
初始值和ID。
当我选择的国家和提交表单一切正常:post_params['country']
等于所选国家的ID,但如果我提交表单,而不改变选择的值(保留默认的一个),然后post_params['country']
helds value
,而不是id
。
如果我错了,如何解决呢?
其中Rails代码创建输入? – 2014-12-05 10:53:07
@МалъСкрылевъ添加的代码创建输入 – 2014-12-05 10:58:35
似乎输入不是一个集合 – 2014-12-05 11:14:03