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