我很难将数据属性设置为在bootstrap selectpicker上选择选项。
我试过了:
$('.selectpicker').on('changed.bs.select', function (e) {
var selected = e.target.value;
console.log("value : ", selected ); // gives selected value
console.log("data attribute: ", $(e.target).data("price"));
});
data属性始终返回undefined
我在这做什么错?
这有效。
$('.selectpicker').on("changed.bs.select", function() {
var dataTypeAttribute = $('option:selected', this).attr("data-type");
});
$('.selectpicker').on('changed.bs.select', function (e) {
var selected = e.target.value;
console.log("value : "+selected ); // gives selected value
console.log("data attribute: "+$(e.target).attr("data-price"));});
只需指定要选择的ID
$("#selectidhere").on("change", function () {
var dataname = $("option[value=" + $(this).val() + "]", this).attr('data-name');
alert(dataname);
});