【问题标题】:Selected listbox item as undefined选定的列表框项目未定义
【发布时间】:2013-09-24 10:55:21
【问题描述】:
<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"})

我尝试从页面加载(在控制器上)以及函数()内部调用fx()。 两者都警报为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>

请帮忙。

【问题讨论】:

    标签: jquery asp.net-mvc-3 listbox selecteditem


    【解决方案1】:

    在fx() 函数中,您正在寻找一个名为resourcename 的select,但您的C# 代码正在创建一个名为CusList 的函数。

    试试这个:

    $(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);            
    }
    

    【讨论】:

    • 谢谢,我使用了两个示例代码,有没有其他方法可以实现列表框选定项而不是使用绑定?
    猜你喜欢
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 2010-11-28
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多