问题
I am implementing the date box in jQuery mobile. I am using the date box in pop up, which is working. I need to set the default date (current date) in the text field when a user opens the pop up screen, using (+) button.
http://jsfiddle.net/ravi1989/uhdYv/
<div data-role="popup" id="CaseInformationScreen" data-close-btn="none" data-overlay-theme="a" data-dismissible="false">
<div data-role="header" data-theme="b" >
<a href="#" data-role="button" data-corners="false" id="Cancel">Cancel</a>
<h1>Case Information</h1>
<a href="#" data-role="button" data-corners="false" id="AddButton">Add</a>
</div>
<div data-role="content">
<div><img src="img/Documents.png"/></div>
<div data-role="fieldcontain">
<label for="text-12" style="text-align:top;margin-left: 0px;">Case Name:</label>
<input name="text-12" id="text-12" value="" type="text" class="caseName_h" >
</div>
<div data-role="fieldcontain">
<label for="text-12" style="text-align:left;margin-left: 0px;" >Case Date:</label>
<!--input name="text-12" id="text-12" value="" type="date" class="caseDate_h" -->
<input name="mydate" id="mydate" type="date" data-role="datebox" data-options='{"mode": "datebox", "useNewStyle":true,"zindex":1200}'/>
</div>
<div data-role="fieldcontain">
<label for="textarea-12">Textarea:</label>
<textarea cols="40" rows="8" name="textarea-12" id="text-12" class="caseTextArea_h"></textarea>
</div>
</div>
</div>
回答1:
To set the Text box's value to current date when the popup opens use this code:
1. Change id of the associated label to myDate:
<label for="myDate" style="text-align:left;margin-left: 0px;" >Case Date:</label>
<input name="mydate" id="mydate" type="date" data-role="datebox" data-options='{"mode": "datebox", "useNewStyle":true,"zindex":1200}'/>
2. JQM code:
$( "#CaseInformationScreen" ).bind({
popupafteropen: function(event, ui) {
var date = new Date();
$('#mydate').trigger('datebox', {
'method': 'set',
'value': date
}).trigger('datebox', {
'method': 'doset'
});
}
});
回答2:
If you're using regular jQuery.
Try this
$('#dateElement').datepicker('setDate', new Date());
Update:
Sounds like you're using DateBox plugin. In that case add "defaultValue", "[2013,1,1]" in your data-options. It takes an array or a string value.
<input name="mydate" id="mydate" type="date" data-role="datebox" data-options='{"defaultValue", "[2013,1,1]", "mode": "datebox", "useNewStyle":true,"zindex":1200}'/>
If you want current date, use it like this.
var currentDate = new Date();
"defaultValue", "[" + currentDate.getFullYear() + "," + currentDate.getMonth() + "," + currentDate.getDate() + "]"
回答3:
If you are using DateBox plugin, the following should work:
<input name="mydate" id="mydate" type="date" data-role="datebox"
data-options='{"defaultValue": [2013,7,10]}'>
回答4:
These two lines should work:
var defaultValue = [year, month, day];
$(element).datebox({'defaultValue' : defaultValue});
来源:https://stackoverflow.com/questions/17716481/how-to-set-the-default-date-on-date-box-in-jquery-mobile-with-datebox-plugin