使用knockout绑定下拉列表value是rewards的id,text是rewards的name,然后自定义两个变量都是 rewards中的属性。
<select data-bind="foreach:rewards" name="rewardNote.reward.id" onchange="addNote()" id="rewardId">
<option data-bind="attr:{note:note,standard:standard},text:name,value:id," >
</option>
</select>
通过jquery的方法能够得到选中项的自定义属性。
function addNote() {
alert($("#rewardId").find("option:selected").attr("note"));
alert($("#rewardId").find("option:selected").attr("standard"));
}
如果是纯html页面比如如下代码,其中note自定义属性。
<select id="rewardId" onchange="addNote()">
<option value="1" note="你好">测试1</option>
<option value="2" note="我好">测试2</option>
<select>
通过jquery一样能够获取,跟上面方法相同!如果不使用jquery,只使用js也能够得到自定义属性的值。如下
window.onload = function() {
var reward = document.getElementById("rewardId");
var note = reward.options[reward.selectedIndex].getAttribute("note");
alert(note);
}