我有一个产品轮播,其中包含多个select
控件和每个控件的图像。每个select
元素都有相同的变化函数:
$('#abcFlavor')
.add('#furyFlavor')
.add('#mrpFlavor')
.add('#proFlavor')
.change(function () {
//some code...
});
这是html的示例:
<div class="shop-latest-carousel">
<ul id="latest_products">
<li>
<form target="" action="..." method="post">
<div class="product-list-item">
<span class="hover"></span>
<div class="image" id="imageABC">
<a>
<img id="ABC_grape" src="images/shop_images/ABC_grape.png" alt=""></a>
</div>
<div class="details fixclear">
<h3>ABC</h3>
<label for="abcFlavor">Flavor:</label>
<select name="os0" id="abcFlavor">
<option id="grape" value="Grape">Grape $2.99 USD</option>
<option id="peach" value="Peach">Peach $22.99 USD</option>
<option id="watermelon" value="Watermelon">Watermelon $12.99 USD</option>
<option id="cherry" value="Cherry">Cherry $12.99 USD</option>
<option id="lemonade" value="Lemonade">Lemonade $5.99 USD</option>
</select>
<div class="actions">
<input type="image" src="~/Images/shop_images/addToCart.png" name="submit" alt="">
</div>
<div class="price">
$<span>52.99</span>
<small>$67.49</small>
<br /><br />
</div>
</div>
</div>
<input type="hidden" name="on0" value="Flavors">
</form>
<!-- end ABC -->
</li>
<li>
<form target="" action="..." method="post">
<div class="product-list-item">
<span class="hover"></span>
<div class="image" id="imageFury">
<a>
<img id="img_fury_lemonlime" src="images/shop_images/Fury_lemonlime.png" alt=""></a>
</div>
<div class="details fixclear">
<h3>Fury</h3>
<label for="furyFlavor">Flavor:</label>
<select name="os0" id="furyFlavor">
<option id="lemonline" value="Lemon-Lime">Lemon-Lime $19.99 USD</option>
<option id="melon" value="Melon">Melon $9.99 USD</option>
</select>
<div class="actions">
<input type="image" src="~/Images/shop_images/addToCart.png" name="submit" alt="">
</div>
<div class="price">
$<span>39.99</span>
<small>$47.24</small>
<br /><br />
</div>
</div>
</div>
<input type="hidden" name="on0" value="Flavors">
</form>
<!-- end Fury -->
</li>
</ul>
</div>
如何对select
触发事件的元素的“id”(不是所选选项的“id”)执行 switch 语句?
我试过这样的事情:
var sel = $(this).parent().attr("id");
但它不断返回“未定义”。