所选列表框项未定义 [英] Selected listbox item as undefined

查看:73
本文介绍了所选列表框项未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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"})

我试图从pageload(在控制器上)以及在function()内调用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>

请帮助.

解决方案

fx()函数中,您正在寻找一个名为resourcenameselect,但是您的C#代码正在创建一个名为CusList的代码. /p>

尝试一下:

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

<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.

解决方案

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

这篇关于所选列表框项未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆