来源:https://zhidao.baidu.com/question/692142321436883524.html
静态的写法:
用jq的attr()函数,如:
HTML:
<select> <option name="aa" data-id="01"></option> <option name="bb" data-id="02" selected="selected"></option> <option name="cc" data-id="03"></option> </select>
JQ:
$(document).ready(function(){ var a = $("select option:selected").attr("name"); var b = $("select option:selected").attr("data-id"); alert(a + " " + b); });
来源:https://www.cnblogs.com/NKing/p/7698133.html
动态的写法:
HTML:
<select class="text-center activate" name="activate" id="activate" > <option data-id="{$vo.Phonecard_id}" data-start="0" value="0" {if condition="$vo.activate eq 0"} selected {/if} >未使用</option> <option data-id="{$vo.Phonecard_id}" data-start="1" value="1" {if condition="$vo.activate eq 1"} selected {/if} >使用中</option> <option data-id="{$vo.Phonecard_id}" data-start="2" value="2" {if condition="$vo.activate eq 2"} selected {/if} >激活中</option> </select>
JQ:
$('.activate').on('change',function(){ var id = $(this).find("option:selected").attr('data-id'); var start = $(this).find("option:selected").attr('data-start'); console.log('id = ' + id); console.log('start = ' + start); })