问题
I am using Jquery mobile, and I have these two select lists, I want to set the selected options for the to lists to the current day and month ..
edit I am using JqueryMobile
the code for 2 lists :
<fieldset data-role="controlgroup">
<legend>تاريخ اخر دورة شهرية : </legend>
<label for="select-choice-day">أختر الشهر</label>
<select name="select-choice-day" id="select-choice-day">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
<!-- etc. -->
</select>
<label for="select-choice-month">اختر الشهر</label>
<select name="select-choice-month" id="select-choice-month">
<option value="1">يناير</option>
<option value="2">فبراير</option>
<option value="3">مارس</option>
<option value="4">إبريل</option>
<option value="5">مايو</option>
<option value="6">يونيو</option>
<option value="7">يوليو</option>
<option value="8">أغسطس</option>
<option value="9">سبتمبر</option>
<option value="10">أكتوبر</option>
<option value="11">نوفمبر</option>
<option value="12">ديسمبر</option>
<!-- etc. -->
</select>
</fieldset>
The code for selecting the current day and month .. but it doesn't work !
var today = new Date();
var day = today.getDate();
var month = today.getMonth();
alert(month);
$('#select-choice-day option').get(day).attr('selected','selected');
$('#select-choice-month option').get(month).attr('selected','selected');
回答1:
The problem with your code is that .get()
on a jquery object returns the DOMElement at the index specified and not a jquery object anymore, so you cannot use .attr() on it (source).
Here's a working piece of code:
$('#select-choice-day option[value=' + day + ']').prop('selected',true);
$('#select-choice-month option[value=' + (month+1) + ']').prop('selected',true);
Please note that .prop()
is only available for jquery 1.6 and above. If you are using an older version of the library, use attr()
as you do.
Here's a jsfiddle
回答2:
Try
var today = new Date();
$('#select-choice-day').val( today.getDate() );
$('#select-choice-month').val( today.getMonth() + 1 );
Remember to add 1
to .getMonth()
since it is zero-indexed.
Here is an example.
回答3:
try this-
$('#select-choice-day option[value=' + day + ']').attr('selected',true);
$('#select-choice-month option[value=' + (month+1) + ']').attr('selected',true);
Working Example
来源:https://stackoverflow.com/questions/8164508/set-select-list-to-current-date-using-jquery