VANHIEP.NET - Làm web giá rẻ - Thiết Kế Website - Thiết Kế Ứng Dụng Mobile
Cách khai báo selected cho thẻ select trong VueJS

Cách khai báo selected cho thẻ select trong VueJS

Cách khai báo selected cho thẻ select trong VueJS

Nếu không dùng v-for mảng data mà show toàn bộ thẻ option thì ta khai báo như sau

<select class="form-control mb-3" name="playStatus" id="">
                                                        <option :value="START" :selected="playStatus[index] == 'START'">START</option>
                                                        <option :value="PLAY" :selected="playStatus[index] == 'PLAY'">PLAY</option>
                                                        <option :value="STOP" :selected="playStatus[index] == 'STOP'">STOP</option>
                                                      </select>

Có index là do ở ngoài thẻ select này có vòng for rồi nên ta sử dụng luôn index của vòng for ngoài để lấy ra các phần tử của mảng.