Selected listbox item as undefined

流过昼夜 提交于 2019-12-13 06:39:34

问题


<script>
    $(function () {
        $("select[name='CusList']").removeAttr('multiple');
        $("select[name='CusList']").attr('size', '8');
        $("select[name='CusList']").find('option:first').attr('selected', 'selected');
        //fx();
    });

    function fx() {
        var resid = $("select[name='resourcename']").val();
        alert(resid);            
    }
</script>

@Html.ListBox("CusList", ((List<SelectListItem>)ViewData["Customer"]), new { @class = "k-list"})

I tried to call fx() from the pageload(on controller) and also inside the function(). Both alert as undefined

<script>
    $(document).ready (function(){
        $('#CusList').bind("click", function () {
            debugger;
            var resid = document.getElementById('CusList');
            var id = resid.options[resid.selectedIndex].value;
            alert(id); //this gives me id correcly, so ly listbox is correct for sure
        });
    });
</script>

Please help.


回答1:


In the fx() function you're looking for a select named resourcename, yet your C# code is creating one called CusList.

Try this:

$(function () {
    $("select[name='CusList']")
        .removeAttr('multiple')
        .attr('size', '8')
        .find('option:first').attr('selected', 'selected');
    fx();
});

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


来源:https://stackoverflow.com/questions/18893277/selected-listbox-item-as-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!