加载时如何为下拉列表中的第一项运行LoadPartial函数?

我有一个函数,当下拉列表更改时-它将加载部分视图.码:

HTML

<p>
    @Html.LabelFor(m => m.SelectedCustomerId, new { @id = "dropdownlabel" })
    @Html.DropDownListFor(m => m.SelectedCustomerId, Model.CustomerIDItem, new { @id = "customId" })
</p>

Javascript:

<script type="text/javascript">
        $(function() {
            $("#customId").change(function() {
                var id = $(this).val();
                $('#divValues').load('@Url.Action("DefaultValuesPartialView", "DefaultValues")?selectedCustomerId=' + id, function (response, status, xhr) {
                    $("#divValues").html(response);
            });
        });
    });
</script>

哪个完美.它加载局部视图(当下拉菜单更改时,更新页面上的表,而无需重新加载整个页面.我要做的是,当加载下拉列表时,它也应该首先加载所选的项目.

我的第一个想法是,它将是这样的:

<script type="text/javascript">
            $(function() {
                $("#customId").onload(function() {
                    var id = $(this).val();
                    $('#divValues').load('@Url.Action("DefaultValuesPartialView", "DefaultValues")?selectedCustomerId=' + id, function (response, status, xhr) {
                        $("#divValues").html(response);
            });
        });
    });
</script>

但是很明显它没有用,有什么想法吗?

提前致谢.

解决方法:

只需在页面加载时触发更改事件

$(function() {
    $("#customId").change(function() {
        var id = $(this).val();
        $('#divValues').load('@Url.Action("DefaultValuesPartialView", "DefaultValues")?selectedCustomerId=' + id);
    }).change(); //trigger change event on page load
});

注意:您不需要使用$(“#divValues”).html(response),因为.load()函数已经为您完成了.

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...