javascript - Jquery year picker not working as expected -
i have created simple jquery year-picker, jelp of post , added custom code make yearpicker show specific value selected. code
html
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <select name="yearpicker" data-id="2010" class="yearpicker"> </select> <select name="yearpicker" data-id="2006" class="yearpicker"> </select>
javascript
$(function () { (i = new date().getfullyear() ; > 1900; i--) { $this = $('.yearpicker'); if ($this.attr("data-id") != i) { $this.append($('<option />').val(i).html(i)); } else { $this.append($('<option selected/>').val(i).html(i)); } } });
what want is, bind property named "data-id"
selection control in end , when script runs want element selected in front end. , have multiple selection controls in page, reason first selections data-id
selected on selections. detailed jsfiddle here. can see 2010 selected in both cases expect 2006 selected on second <select/>
can 1 point out doing wrong?
try this:
$(function () { $('.yearpicker').each(function(){ (i = new date().getfullyear() ; > 1900; i--) { $this = $(this); if ($this.attr("data-id") != i) { $this.append($('<option />').val(i).html(i)); } else { $this.append($('<option/>').val(i).attr("selected", "selected").html(i)); } } }); });
Comments
Post a Comment