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

请帮忙。

4

1 回答 1

1

fx()您正在寻找一个select名为的函数中resourcename,但您的 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);            
}
于 2013-09-19T11:26:36.080 回答