select day , month and year by jsondata

success: function(jsondata){

            var data=JSON.parse(jsondata);

                var list_html="<div id='editrelation'><label id='dateLabel' style='display:none'>Since/Anniversary</label><select style='display:none' id='month' class=''  aria-label='Month' name='month'><option value='-1'>Month:</option><option value='1'>Jan</option><option value='2'>Feb</option><option value='3'>Mar</option><option value='4'>Apr</option><option value='5'>May</option><option value='6'>Jun</option><option value='7'>Jul</option><option value='8'>Aug</option><option value='9'>Sep</option><option value='10'>Oct</option><option value='11'>Nov</option><option value='12'>Dec</option></select><select id='day' class='' aria-label='Day' name='day'  style='display:none; margin-left: 74px; margin-top: -25px;'><option value='-1'>Day:</option>";
                for(var i=1;i<=31;i++){
                list_html+="<option value="+i+" id='d"+i+"'>"+i+"</option>";
                }
                list_html+="</select><select style='display:none; margin-top: -25px; margin-left: 130px;' id='year' class='' aria-label='Year' name='year'><option value='-1'>Year:</option>";
                    for(var i=2014;i>=1914;i--){
                        list_html+="<option value="+i+" id="+i+">"+i+"</option>";
                        }
                list_html+="</select>"
                list_html+="</div>";
                document.getElementById('form_relation').innerHTML=list_html;
                if(data['date']!=undefined){//data['date']='1/24/1990'
                    var ani=data['date'];
                    var dateArray=ani.split("/");
                    var month=dateArray[0];
                    var day=dateArray[1];
                    var year=dateArray[2];
                        document.getElementById("month").selectedIndex =month;
                        document.getElementById("day").selectedIndex =day;
                        document.getElementById("year").selectedIndex =year;

                }
}

below line is not working

document.getElementById("year").selectedIndex =year;
有帮助吗?

解决方案

try with 'value'

document.getElementById("month").value =month;
document.getElementById("day").value=day;
document.getElementById("year").value=year;

其他提示

This can't work. In 2 words, the elements you're trying to access are not in the DOM, they're not even in the document yet, so there's no way to get it through getElementById(). you should remove the whole loop building those elements and simply put them in the page, they don't use the returned data anyway...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top