javascript - 从部分 View 的 javascript 函数调用父 View 的 javascript 函数

标签 javascript asp.net-mvc

我在父 View 中有一个 JavaScript 函数 Check(),并且该父 View 有一个名为 child 的部分 View 。这个 subview 中有一个 java 脚本函数,该函数将在 anchor 标记单击后触发,然后调用我的父 View 的 Check() 函数。

所以我需要的是从这个部分 View 的函数中调用父 View 的 Check() 函数。 简单地将父 View 的脚本函数放入局部 View 中并不会调用Check()。我该怎么做,请纠正我

这就是我到目前为止所做的,

我的部分 View 脚本函数看起来像

function supp_checkAndReset(elem) {
    var currentSelected = $(elem).attr('id');
    $('.supp_panelfilterresultholder span[title="' + currentSelected + '"]').remove();
    $('.supp_panelfiltersuggestion input[value="' + currentSelected + '"]').prop('checked', false);

    Check();
 }

我的父 View 脚本函数位于脚本标记下,看起来像

 function Check() {
        var supp_fullCheckList = $("#supp_filterWithThis").val();
        var practice_fullCheckList = $("#practice_filterWithThis").val();
        var county_fullCheckList = $("#county_filterWithThis").val();
        var state_fullCheckList = $("#state_filterWithThis").val();

        var ratebase_sliderValue = $("#ratebase_sliderValue").val();
        var clientRating_sliderValue = $("#clientRating_sliderValue").val();
        var panelRating_sliderValue = $("#panelRating_sliderValue").val();

        $.ajax({
            url: '@Url.Action("GetSearchResultBasedOnFilters", "SearchResult")',
            type: 'GET',
            data: {
                supp_fullCheckList: supp_fullCheckList, practice_fullCheckList: practice_fullCheckList, county_fullCheckList: county_fullCheckList,
                state_fullCheckList: state_fullCheckList, ratebase_sliderValue: ratebase_sliderValue, clientRating_sliderValue: clientRating_sliderValue, panelRating_sliderValue: panelRating_sliderValue
            },
            success: function (response) {
                var totRecordCount = $('#RecordTotalCount').val();

                $('#TotRecordCount').html(totRecordCount);

                $('#searchArea').html('');
                $('#searchArea').html(response);
            },
        error: function (xhr, status, error) {
            alert(status + " : " + error);
        }});
    }

最佳答案

我也遇到了同样的问题。将 javascript 函数 supp_checkAndReset(elem)Check() 保留在主视图(即父 View )中。因此,它可以从 View 和部分 View 访问。

父 View 中应该是这样的:

<script type="text/javascript">
function supp_checkAndReset(elem) {
var currentSelected = $(elem).attr('id');
$('.supp_panelfilterresultholder span[title="' + currentSelected + '"]').remove();
$('.supp_panelfiltersuggestion input[value="' + currentSelected + '"]').prop('checked', false);

Check();
}
 function Check() {
    var supp_fullCheckList = $("#supp_filterWithThis").val();
    var practice_fullCheckList = $("#practice_filterWithThis").val();
    var county_fullCheckList = $("#county_filterWithThis").val();
    var state_fullCheckList = $("#state_filterWithThis").val();

    var ratebase_sliderValue = $("#ratebase_sliderValue").val();
    var clientRating_sliderValue = $("#clientRating_sliderValue").val();
    var panelRating_sliderValue = $("#panelRating_sliderValue").val();

    $.ajax({
        url: '@Url.Action("GetSearchResultBasedOnFilters", "SearchResult")',
        type: 'GET',
        data: {
            supp_fullCheckList: supp_fullCheckList, practice_fullCheckList: practice_fullCheckList, county_fullCheckList: county_fullCheckList,
            state_fullCheckList: state_fullCheckList, ratebase_sliderValue: ratebase_sliderValue, clientRating_sliderValue: clientRating_sliderValue, panelRating_sliderValue: panelRating_sliderValue
        },
        success: function (response) {
            var totRecordCount = $('#RecordTotalCount').val();

            $('#TotRecordCount').html(totRecordCount);

            $('#searchArea').html('');
            $('#searchArea').html(response);
        },
    error: function (xhr, status, error) {
        alert(status + " : " + error);
    }});
}

关于javascript - 从部分 View 的 javascript 函数调用父 View 的 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31018057/

相关文章:

javascript - Angularjs ngRoute 不工作 : Uncaught Error: [$injector:modulerr]

javascript - 命名参数和 JavaScript 中的参数对象

javascript - 如何更有效地做到这一点?

c# - 路由在 mvc 中不起作用

c# - 来自弹出表单的 ajax 数据发布未命中 Controller 方法 ASP.NET MVC

asp.net-mvc - 处理程序 "dotless"在其模块列表中有一个错误模块 "ManagedPipelineHandler"

javascript - 如何使用带有模糊和焦点的 iframe

javascript - Safari 的 getBoundingClientRect() 实现返回不正确的顶部

asp.net-mvc - 从 MVC3 迁移到 MVC4 和 Azure 1.7 - 为什么 iisexpress.exe 现在出现在日志文件中

c# - 如何使用 StackExchange.Redis 存储列表?