简体   繁体   中英

How to set the right value as selected after populating multiple dropdowns?

As the title says, I'm trying to update dropdowns I've populated on a timesheet with jQuery every time I load my table. So there are like 28-31 days and each day has its own dropdown with id="dayType1" all the way up to 31. I was hoping there is a way to iterate on the jQuery selector but from what I read it is not possible? I tried to put each time it runs over the iteration a new ID together like a string and then paste it into the function as a selector, but that doesn't seem to work either. So now I call upon the jQuery gods here to help this peasant. :)

I keep the values for the dropdowns in a separate list so as the iteration goes the list has the values that I need to change the dropdowns to in order at the same spot. (So dropdown 8 needs to be set as the integer saved in the list on the 8th spot.)

First I had timesheets of only a week and could do this:

function setDayTypes() {
    $('#dayType1').val(dayTypeList[0]);
    $('#dayType2').val(dayTypeList[1]);
    $('#dayType3').val(dayTypeList[2]);
    $('#dayType4').val(dayTypeList[3]);
    $('#dayType5').val(dayTypeList[4]);
    $('#dayType6').val(dayTypeList[5]);
    $('#dayType7').val(dayTypeList[6]);
}

But now I have timesheets of a month and not all months are the same. Is there a clean way to iterate over these id's? In the dayTypeList are the values for the inputs.

在此处输入图像描述

This works great, thanks.

$( "td .dayType" ).each(function( index ) {
   $(this).val(dayTypeList[index])
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM