我遇到了下拉列表中出现双值的问题,这与我想要的相反。
我有一个带有下拉列表的购物车,用户可以从中选择以价格作为其值的cakesize
。
我的设置是将购物车存储在浏览器中的会话中,以便存储他选择的值。
我的问题是,当我加载页面时,所选cakesize
在所选选项和列表中出现两次。
<select name="cakesize" class="custom-select cakesize">
<option selected value="<%=cake.currentPrice%>"><%=cake.currentCakeSize %></option>
<option value="<%=cake.item.price['1000'] %>">1kg</option>
<option value="<%=cake.item.price['1500'] %>">1.5kg</option>
<option value="<%=cake.item.price['2000'] %>">2kg</option>
<option value="<%=cake.item.price['2500'] %>">2.5kg</option>
<option value="<%=cake.item.price['3000'] %>">3kg</option>
<option value="<%=cake.item.price['3500'] %>">3.5kg</option>
<option value="<%=cake.item.price['4000'] %>">4kg</option>
<option value="<%=cake.item.price['4500'] %>">4.5kg</option>
<option value="<%=cake.item.price['5000'] %>">5kg</option>
</select>
保持这种状态
可以吗?正确的方法如下
<select name="cakesize" class="custom-select cakesize">
<option value="<%=cake.item.price['1000'] %>" <%=cake.currentPrice == cake.item.price['1000'] ? 'selected' : '' %> >1kg</option>
<option value="<%=cake.item.price['1500'] %>" <%=cake.currentPrice == cake.item.price['1500'] ? 'selected' : '' %> >1.5kg</option>
<option value="<%=cake.item.price['2000'] %>" <%=cake.currentPrice == cake.item.price['2000'] ? 'selected' : '' %> >2kg</option>
<option value="<%=cake.item.price['2500'] %>" <%=cake.currentPrice == cake.item.price['2500'] ? 'selected' : '' %> >2.5kg</option>
<option value="<%=cake.item.price['3000'] %>" <%=cake.currentPrice == cake.item.price['3000'] ? 'selected' : '' %> >3kg</option>
<option value="<%=cake.item.price['3500'] %>" <%=cake.currentPrice == cake.item.price['3500'] ? 'selected' : '' %> >3.5kg</option>
<option value="<%=cake.item.price['4000'] %>" <%=cake.currentPrice == cake.item.price['4000'] ? 'selected' : '' %> >4kg</option>
<option value="<%=cake.item.price['4500'] %>" <%=cake.currentPrice == cake.item.price['4500'] ? 'selected' : '' %> >4.5kg</option>
<option value="<%=cake.item.price['5000'] %>" <%=cake.currentPrice == cake.item.price['5000'] ? 'selected' : '' %> >5kg</option>
</select>