Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 加载时如何为下拉列表中的第一项运行LoadPartial函数?_Javascript_Jquery_Html_Asp.net Mvc - Fatal编程技术网

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

Javascript 加载时如何为下拉列表中的第一项运行LoadPartial函数?,javascript,jquery,html,asp.net-mvc,Javascript,Jquery,Html,Asp.net Mvc,我有一个功能,当下拉菜单被更改时,它会加载一个局部视图。代码: Html: <p> @Html.LabelFor(m => m.SelectedCustomerId, new { @id = "dropdownlabel" }) @Html.DropDownListFor(m => m.SelectedCustomerId, Model.CustomerIDItem, new { @id = "customId" }) </p> <sc

我有一个功能,当下拉菜单被更改时,它会加载一个局部视图。代码:

Html:

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

@LabelFor(m=>m.SelectedCustomerId,新的{@id=“dropdownlab”})
@Html.DropDownListFor(m=>m.SelectedCustomerId,Model.CustomerIDItem,new{@id=“customId”})

Javascript:

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

$(函数(){
$(“#customId”).change(函数(){
var id=$(this.val();
$('#divValues').load('@Url.Action(“DefaultValuesPartialView”,“DefaultValues”)?selectedCustomerId='+id,函数(响应,状态,xhr){
$(“#divValues”).html(响应);
});
});
});
这很好用。它加载部分视图(当下拉列表被更改时更新页面上的表,而不重新加载整个页面。我想做的是,当下拉列表被加载时,它也应该首先加载所选的项目

我的第一个想法是这样的:

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

$(函数(){
$(“#customId”).onload(函数(){
var id=$(this.val();
$('#divValues').load('@Url.Action(“DefaultValuesPartialView”,“DefaultValues”)?selectedCustomerId='+id,函数(响应,状态,xhr){
$(“#divValues”).html(响应);
});
});
});
但很明显,它不起作用,有什么想法吗


提前感谢。

只需在页面加载时触发
change
事件即可

$(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(响应)
作为
.load()
函数已经为您完成了。

@brothers28有什么想法吗?