我的HTML表单中有一个<select>
下拉列表。如何将required
属性添加到我的<select>
元素中?我想要一个纯HTML5解决方案。我用Google搜索,但我似乎找不到它。
答案 0 :(得分:2)
第一个<option>
需要为空。否则,required
会将其视为已填充的值。
<强> Example 强>
<select required>
<option></option>
<option>1</option>
<option>2</option>
</select>
答案 1 :(得分:0)
首先,您必须在电影列表中插入空字段。并添加required
<强> HTML 强>
<form id="book_tickets">
<fieldset>
<legend>Booking Details</legend>
<p>
<label for="movie_name">Choose Movie</label>
<select id="movie_name" name="movie_name" required>
<option></option>
<option value="Movie 1">Movie 1</option>
<option value="Movie 2">Movie 2</option>
<option value="Movie 3">Movie 3</option>
</select>
</p>
<p>
<label for="theaters">Theaters</label>
<input list="theaters" name="theaters" required>
<datalist id="theaters">
<option value="Inox" />
<option value="E Square" />
</datalist>
</input>
</p>
<p>
<label for="date">Select Date</label>
<input type="date" name="date" id="date" required />
</p>
<p>
<label for="email">Enter Email</label>
<input type="email" name="email" id="email" placeholder="abc@gmail.com" required />
</p>
<p>
<label for="tickets_quantity">Number of Tickets</label>
<input type="number" min="1" max="10" name="tickets_quantity" id="tickets_quantity" required />
</p>
<p>
<label>Total Price</label>
<span id="total_price"></span>
</p>
<p>
<input type="submit" id="book_tickets" value="Book Tickets" />
</p>
</fieldset>
</form>
答案 2 :(得分:0)