簡體   English   中英

如何在加載列表框時選擇第一個項目

[英]how to get the first item selected on loading the listbox

我有這個列表框從數據庫加載項目現在我需要第一個項目保持選擇加載它,請幫助。 我在jquery嘗試但是沒有幫助。

@model  proj.Models.ListboxViewModel
@Html.ListBoxFor( m=> m.resourcename,Model.resourcename, new { @class = "resList",style="height: 121px;"})

<script>
    $(function () {
        $("select[name='resourcename']").removeAttr('multiple');
        $("select[name='resourcename']").attr('size', '8');
     //   $("#resourcename").find("option").attr("selected", '1'); // this is not working
   });
</script>

這可以在JQuery中快速完成

JQuery的

$(function () {
    $("select[name='resourcename']").find('option:first').attr('selected', 'selected');
});

要使用jquery嘗試獲取選擇框的值。

function getValue() {
    var resid = $("select[name='resourcename']").val();
    alert(resid)
}

干杯

嘗試使用Jquery函數.prop() ,是最安全的方法:

$("select[name='resourcename']").find("option:first").prop('selected',true);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM