简体   繁体   English

Rails将两个表单字段组合为一个

[英]Rails combined two form fields into one

How do I combined the day and time into a "start_time" variable (and "end_time") before submitting? 在提交之前,如何将日期和时间合并到“ start_time”变量(和“ end_time”)中? JS? JS?

 <%= form_for @task do |f| %>
        <%= render 'shared/error_messages', object: f.object %>
          <div class="time_selectors">
            <%= f.label :start_time %>
            <%= f.text_field :start_time, :class => 'datepicker' %>
            <select class="form-control time_dropdown" id="start_hour">
              <option>Select Date</option>
            </select>
            <div class="clear_both"></div>
            <%= f.label :end_time %>
            <%= f.text_field :end_time, :class => 'datepicker' %>
            <select class="form-control time_dropdown" id="end_hour">
              <option>Select Date</option>
            </select>
            </div>
            <div class="clear_both"></div>
          </div>
          <div class="clear_both"></div>
          <%= f.submit "Create Task", class: "btn btn-large btn-primary" %>
        </div>
    <% end %>

I have them combined in my Task model, and I'd prefer to leave them combined if I can. 我将它们合并在我的任务模型中,如果可以的话,我宁愿将它们合并。

My JS right now doesn't work. 我的JS现在无法正常工作。 What am I missing? 我想念什么?

$('#new-task').submit ()->
  valuesToSubmit = $(this).serialize
  console.log('test')
  console.log(valuesToSubmit)
  return false
$('button').click ()->
  console.log ('test2')

What about something like this? 那这样的东西呢?

$('form').on 'submit', (event) ->
    event.preventDefault()
    start_time = ''
    start_time += $(@).find('input[name=start_day]').val()
    start_time += '_'
    start_time += $(@).find('input[name=start_hour]').val()
    $('<input/>', {type: 'hidden', name: 'start_time', value: start_time}).appendTo($(@))
    @submit()

(*The CoffeeScript version hasn't been tested - use the working Fiddle as your guide.) (* CoffeeScript版本尚未经过测试-使用工作中的Fiddle作为指导。)

Fiddle 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM