0

如何使用表单中的选定值进行警报?

有没有办法得到它getElementById或等效的?

形式

= f.select :some_key, some_array
`= f.submit 'Submit', data: { confirm: t('views.confirm.caution', :choice => some_array[<CHOSEN INDEX HERE>]) }`

en.yml

en:
  views:
    confirm:
      caution: "Are you sure to submit %{choice} ?"

警报

4

1 回答 1

0

en.yml

en:
  views:
    confirm:
      caution: "Are you sure to submit {{choice}} ?"

form.slim

= f.select :some_key, some_array
- # other code
= f.submit 'Submit', data: { confirm: '' ) }`

:javascript
  document.getElementById('your_select_id').addEventListener('change', function(){
    var value = '' // get select value
    var btn = document.getElementById('you confirm button id')
    var original_message = "#{t('views.confirm.caution')}"
    var confirm_message = original_message.replace('{{choice}}', value)
    btn.setAttribute('data-confirm', confirm_message)
  })
于 2021-05-27T09:53:35.543 回答