readonly text input on focus rails3
I'd followed the railscast 213 (calendars) from Ryan Bates
Where he makes use of a text input field to popup a date picker, this works fine. I want to give limitations on the input date so gave the following attributes:
$(function() {
$("#question_deadline").datepicker({ duration: 'fast', maxDate: '+2m', minDate: 'now' });
});
Now when i press in the textfield the date picker pops up, but i still can change the date by typing in the textfield... So i thought i put the readonly option on true in the _form.html.erb file for this particular field, this works but the date does not write to the database.
So then i made this in the application.js file:
$(function() {
$("#question_deadline").datepicker({ duration: 'fast', maxDate: '+2m', minDate: 'now' });
});
$("#question_deadline").focus {
function()
$("#question_deadline").attr("readonly", true);
}
$("#question_deadline").blur {
function()
$("#question_deadline").removeAttr("re开发者_Python百科adonly");
}
But this prevents the date picker from working at all?
Got it allready:
$(function() {
$("#question_deadline").focus(function() {
$("#question_deadline").attr("readonly", true);
});
});
$(function() {
$("#question_deadline").blur(function() {
$("#question_deadline").removeAttr("readonly");
});
});
Regards, Thijs
I suppose you may intercept an "onChange"
event, and validate the value in the control. Forcing clients to always use a datepicker may not necessarily be welcome by all of them.
I personally, often prefer to type the date instead of select it.
If you validate the date, and the value happens to be invalid, you may force the user back and display the datepicker. I humbly believe that would be a more user friendly approach.
精彩评论