简体   繁体   中英

Rails input returning false

I have an input field with type="date" for a user to input a date, however, I need the value of this to go into a hidden input field in rails.

This is what I'm trying and the code... (the disabled attribute is removed on a checkbox being checked, this is not the issue)

<input disabled id="scheduled-date" class="form-control" type="date" value="<%= f.object.published_on %>"></input>

<%= f.input :published_on, as: :hidden, input_html: { value: f.object.published_on } %>

So the Input 1 had the value of, for example, '28/10/2017' however, the rails input field is shown as false.

I am unsure where I have gone wrong and why the values are different.

even if the code you write may be correct, you still need javascript to update that field with the value when the user clicks on the <input id='scheduled-date'> and changes/input the date. This is not the best code, but an example of what you need.

Every time the user focus comes out of that input field with id='scheduledate' you need to get the new value and change the value from the other rails input field with id='published_on'

$('#schedule-date').focusout(function(){
     var value = $('#schedule-date').value();
     $('#published_on').value(value);
})

Still you may have other errors in your solution, but the basic idea is, the way you are designing it, it will change only that input field when the server end the get http request, so when the first time the page is loaded, that { value: f.object.published_on } is set, while my jquery works every time the user brings his focus outside from that input field.

If your code is working, it is doing so only at the end of the first http get request (the end of the loading of the page), you can test this by giving

<input disabled id="scheduled-date" class="form-control" type="date" value="<%= f.object.published_on %>"></input>

a value so that when it loads the page also the other input field

<%= f.input :published_on, as: :hidden, input_html: { value: f.object.published_on } %>

get that value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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