选定的列表框项目未定义
$(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); } @Html.ListBox("CusList", ((List)ViewData["Customer"]), new { @class = "k-list"})
我试图从pageload(在控制器上)和函数()内部调用fx()
()。 两者都警告为undefined
$(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 }); });
请帮忙。
在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); }