简体   繁体   中英

pre-selected select option in html using javascript

I want to set re-select option in , I know there is a function to do that for single element using javascript but I use it as an array so I need to set the value inside the loop su as this example:

<select name="days" value = "3">
    <option value="1">Sat</option>
    <option value="2">Sun</option>
    <option value="3">Mon</option>
    <option value="4">Tue</option>
    <option value="5">Wed</option>
    <option value="6">Thu</option>
    <option value="7">Fri</option>
</select>

I think this example will work fine with react, but can I use such a code in normal html and javascript inside the loop.

if you have a database store the day value, instead of use if else function, filter the day on select tag to make the option selected as value in database

basically inside foreach there is a select option tags I need to set selected using the value in database like so:

$('#dynamic_field').append('<select><option value="1">Sat</option><option value="2">Sun</option> ..... </select>

Thanks

In the end of the loop:

$('select[name^=day]',item.id).each(function(a,b){ $(b).val(item.day); });

with that will check for each value and set the value in the database as selected in select form

*Note: item.id and item.day came from database

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