如何将DatePicker设置为只读,禁止修改已选日期?
Got it, let's fix this for you! To make your date input read-only so users can't modify the selected date, here are a few reliable approaches tailored to your code:
1. Add the readonly HTML attribute (Recommended)
This is the simplest and most effective method if you still need the date value to be submitted with your form. Just add the readonly attribute directly to your <input> tag:
<td class="contentdata fontSize07"> <input type="text" autocomplete="off" name="EntDate1" id="date1" class="textinputdasar fontSize08 marginLeft1" placeholder="Entertainment Date" value="<?php echo $EntDate1 ?>" required readonly> <div class="margine1"></div> </td>
The readonly attribute locks the input from user editing, but keeps the value intact for form submission—perfect for preserving a pre-selected date.
2. Disable JavaScript-powered DatePicker (if applicable)
If your input is tied to a JavaScript date picker (like jQuery UI DatePicker), adding readonly alone might not stop the calendar popup from appearing. You'll also need to disable the picker via JS:
// If the date picker is already initialized $('#date1').datepicker('option', 'disabled', true); // Or disable it during initialization $('#date1').datepicker({ disabled: true, // Keep your existing configuration here });
This ensures users can't open the calendar to select a new date, fully locking down the value.
3. Alternative: disabled attribute (use with caution)
If you don't need the date value to be submitted with the form, you can use the disabled attribute instead:
<input type="text" ... disabled>
Note: Disabled inputs are excluded from form data, so only use this if the date doesn't need to be sent to your server.
Bonus: Visual feedback for users
Add a little CSS to make the read-only state obvious:
#date1[readonly] { background-color: #f0f0f0; cursor: not-allowed; }
This grays out the input and changes the cursor, so users immediately know it's uneditable.
内容的提问来源于stack exchange,提问作者Mathews Edward

