簡體   English   中英

頁面加載調用事件的兩個下拉列表更改

[英]On page load call change of events for both dropdown

在首次加載頁面時,將顯示幫助文本和公告,在驗證后刷新時,幫助文本和公告不會再次顯示在視圖上。 我想我都需要在頁面加載調用更改事件中都將其下拉,我不確定如何做到這一點。 第一個下拉菜單的Div id是#profession,第二個下拉菜單的div id是#enquirytype。

$('#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();
                }
            });
        });


        $('#enquirytype').on('change', function (e) { //onlick of professions DD
            var selectedVal = $(this).val(); //Variable selectedVal this .value
            $('#enquiryTypeHelpAlert').hide(); ///////
            $('#EnquiryTypeAnnouncements').empty();
            if (selectedVal != undefined && selectedVal != '') {
                $.ajax({
                    type: 'GET', //Get 
                    url: "@Url.Action("GetEnquiryTypeAndAnnoncements", "UnauthEnquiry")", //It goes to the enquiry controller method GetenquiryTypes
                    dataType: 'json', //Datatypes JSON
                    data: { SelectedEnquiryType: selectedVal }, //data is SelectedProfession: selectedVal
                    success: function (json) { //Jquery Parse Json data from server on ajax success

                        if (json.helptext != undefined && json.helptext != '') {
                            $('#enquiryTypeHelp').html(json.helptext)
                            $('#enquiryTypeHelpAlert').show(); ///////
                        }
                        else
                            $('#enquiryTypeHelpAlert').hide(); ///////
                        var announcement = $('.EnquiryTypeAnnouncement:first').clone();
                        $('#EnquiryTypeAnnouncements').empty();
                        $('#enquiryTypeHelp').html(json.helptext);
                        for (var i in json.announcements) {
                            var announcementCopy = announcement.clone();
                            announcementCopy.find(".notification").html(json.announcements[i]);
                            $(announcementCopy).appendTo($('#EnquiryTypeAnnouncements')).show();
                            $('#EnquiryTypeAnnouncements').show();
                        }


                    }
                });
            }

        });

在ypur腳本的開頭,請使用以下函數調用您的Professions下拉列表

$(document).ready(function () {
$('#profession').change(); //Keeps profession dropdown list help text displayed
});

接下來,因為查詢類型在Jquery中不可用。 您可以從Model那里得到。 通過使用

var EnquiryType ='@Model.EnquiryType

然后在change事件中獲取它。

這似乎是正確的,因為進行更改將使您的DD幫助文本保持加載狀態。

$(document).ready(function () {
$('#profession').change(); //Keeps profession dropdown list help text displayed
});

由於它不在Jquery中,因此必須從模型中獲取它。

var EnquiryType ='@Model.EnquiryType

然后在change事件中獲取它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM