页面加载调用更改事件?

时间:2016-10-03 15:40:54

标签: javascript jquery asp.net-mvc

在页面加载时,我需要为下拉列表调用更改事件。目前发生的事情是在我的Jquery代码中,当您从专业下拉列表中选择一个值,即GP,帮助文本然后在DIV区域的下拉列表下方显示,但是一旦您提交表单并且如果验证被抛出帮助文字来自视图。

我需要为下拉列表#profession调用更改事件,但不确定如何执行此操作。请指教

    $('#profession').on('change', function (e) { //Gets the ID of profession drop down list
        var selectedVal = $(this).val(); //Variable selectedVal this . value
        $.ajax({ //Ajax declared
            type: 'GET', //Its a get
            url: "@Url.Action("GetenquiryTypes", "UnauthEnquiry")", //It goes to the enquiry controller method GetenquiryTypes
            dataType: 'json', //Datatypes JSON
            data: { SelectedProfession: selectedVal }, //data is SelectedProfession: selectedVal
            success: function (json) { //Jquery Parse Json data from server on ajax success
                if (json.helptext != undefined && json.helptext != '')
                    {
                    $('#ProfHelp').html(json.helptext)
                    $('#ProfHelpAlert').show(); ///////
                }
                else
                    $('#ProfHelpAlert').hide(); ///////

                var targetDropdown = $('#enquirytype') //Var targetDropDropdown goes to dropdown ID enquiry type
                targetDropdown.empty(); //target empty dropdown
                $("<option />", {
                    val: "",
                    text: "Please select enquiry type" //Select enquiry type
                }).appendTo(targetDropdown); //add to the target dd
                if (json.enquiryTypes.length > 0) { //if JASON data from server greater then 0
                    for (var EnquiryType in json.enquiryTypes) { //go through each EnquiryType in JSON
                        $("<option />", {
                            val: json.enquiryTypes[EnquiryType].EnquiryId, //mapping
                            text: json.enquiryTypes[EnquiryType].Enquiryname //mapping
                        }).appendTo(targetDropdown); //add to drop down
                    };
                }
                targetDropdown.change();
            }
        });
    });

1 个答案:

答案 0 :(得分:0)

试试这个:

$(document).ready(function() {
    $('#profession').trigger('change');
});