根据 Collection_select select 菜单设置隐藏字段值

Set Hidden Field Value Based on Collection_select select menu

我正在使用 select2,我有一个 collection_select 的更新表单,如下所示:

<%= collection_select(:event, :project_date_id, @project_dates.available, :id, :schedule_date, :prompt => false, :selected => @set_date.id)  %>

选择选项(:ID)时,我还要将显示的值(:schedule_date)放在隐藏的字段中。

<%= f.hidden_field :starts_on, value: {HERE'S WHERE the :schedule_date display value from the select goes}



我觉得这是 Jquery 的工作...但我对 Jquery 还是个新手,搞不懂。

试试这个

<script>
    $(function () {
        $("#select_id").change(function () {
            $('#hidden_field_id').val(this.value);
        });
    });
</script>

我不是 ruby 开发人员,但如果是 jquery,您可以这样做:

<script>
  $(function(){
    $(document).on('change','#_id_name_for_select', function(){
       var select_value = $(this).val();
       $('#_id_name_for_hidden_field').val(select_value);

    });
  });
</script>

用于下拉(select 字段)ID:

<script type="text/javascript">
    $(document).on('change','#select_id', function(){
        var select_value = $(this).select2('val');
        $('#hidden_field_id').val(select_value);
    });
</script>

用于下拉(select 字段)class:

<script type="text/javascript">
    $(document).on('change','.select_class', function(){
        var select_value = $(this).select2('val');
        $('#hidden_field_id').val(select_value);
    });
</script>