Jquery 获取select选中项 自定义属性的值


   <select class="select" size="1" name="teacher" id="teacher">
                                <option value="">请选择教师</option>
                                @{
                                    foreach (var teach in ViewBag.teacherlist)
                                    {
                                        <option value="@teach.id" data-username="@teach.username" data-userid="@teach.user_id">@teach.realname_cn</option>
                                    }
                                }
                            </select>

JS 取值

第一种取值
$('#select1').find("option:selected").attr("自定义属性")

"teachername": $('#teacher').find("option:selected").text(), 取名称
"teacherid": $('#teacher').val(), "teacheruserid": $('#teacher').find("option:selected").attr("data-userid"), "teacherusername": $('#teacher').find("option:selected").attr("data-username")
第二种取值
如果你的属性定义的是data-xxx,那么直接
$('#select1').find("option:selected").data("xxx");
$('#teacher').find("option:selected").data("userid")


 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM