获取option自定义属性

HTML

1
2
3
4
5
<select name="choose-fruits" id="fruits" >
<option value="Apple" data-sku="fruits-1">Apple</option>
<option value="Orange" data-sku="fruits-2">Orange</option>
<option value="banana" data-sku="fruits-3">banana</option>
<select>

JavaScript

1
2
3
4
5
6
7
8
let fruitList = document.getElementById("fruits");
let index = fruitList.selectedIndex;
let fruitSku = fruitList.options[index].getAttribute("data-sku")
fruitList.onchange = () => {
index = fruitList.selectedIndex;
fruitSku = fruitList.options[index].getAttribute("data-sku")
console.log(fruitSku);
}

jQuery

1
2
3
4
5
6
// 直接获取
console.log($('#fruits').find("option:selected").attr("data-sku"));
// onChange
$('#fruits').change(function () {
console.log($(this).find("option:selected").data('sku'));
})
载入天数...载入时分秒...

请我喝杯咖啡吧~

支付宝
微信