级联功能的多个参数

时间:2013-06-27 22:43:27

标签: javascript jquery asp.net-mvc

我需要在级联函数中传递多个参数:

// Cascade function
(function ($) {
    $.fn.cascade = function (options) {
        var defaults = {};
        var opts = $.extend(defaults, options);
        return this.each(function () {
            $(this).change(function () {
                var selectedValue = $(this).val();
                if (selectedValue == '') {
                    opts.childSelect.empty();
                    return;
                }
                var params = {};
                params[opts.paramName] = selectedValue;
                $.post(opts.url, params, function (items) {
                    opts.childSelect.empty();
                    if (opts.firstOption != "")
                        opts.childSelect.append(
                            $('<option/>')
                                .attr('value', '')
                                .text(opts.firstOption));
                    $.each(items, function (index, item) {
                        opts.childSelect.append(
                            $('<option/>')
                                .attr('value', item.Id)
                                .text(item.Name)
                        );
                    });
                    if (typeof (opts.callback) == "function") { opts.callback(); }
                });
            });
        });
    };
})(jQuery);

在通话中,我需要传递makeId和categoryId值:

// bind cascade Make dropdown
$("#Make_Id").cascade({
    url: "/Ad/ListModelByCategoryByMake",
    paramName: "makeId",
    firstOption: 'Selecione o Modelo...',
    childSelect: $("#Model_Id")
});

通过post进行调用,那么如何使用两个参数调用cascade函数?

感谢。

1 个答案:

答案 0 :(得分:0)

功能:

$.fn.cascade = function (options, param2) {
    /// ....
}

呼叫:

$("#Make_Id").cascade({
    url: "/Ad/ListModelByCategoryByMake", // is better to use @Url.Action() helper here
    paramName: "makeId",
    firstOption: 'Selecione o Modelo...',
    childSelect: $("#Model_Id")
}, 'your_second_param');