本文介绍了使用JQuery更新Html.Dropdownlists的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这里这个解决方案http://stackoverflow.com/questions/2782815/how-to-update-strongly-typed-html-dropdownlist-using-jquery而想实现它,但东西是在它窃听。

I found this solution on here http://stackoverflow.com/questions/2782815/how-to-update-strongly-typed-html-dropdownlist-using-jquery and am trying to implement it, but something is bugged in it.

$(function() {
    $('#cid').change(function() {
        var selectedCompany = $(this).val();
        var ddl = $("#foid");
        $.post("/TimeTracking/FilterFieldOffices", { companyId: selectedCompany }, function (data) {
            $(ddl).loadSelect(data);
        });
    });
});

(function($) {
    $.fn.emptySelect = function() {
        return this.each(function() {
            if (this.tagName == 'SELECT') this.options.length = 0;
        });
    }

    $.fn.loadSelect = function(optionsDataArray) {
        return this.emptySelect().each(function() {
            if (this.tagName == 'SELECT') {
                var selectElement = this;
                $.each(optionsDataArray, function(index, optionData) {
                    var option = new Option(optionData.Text, optionData.Value);

                    if ($.browser.msie) {
                        selectElement.add(option);
                    }
                    else {
                        selectElement.add(option, null);
                    }
                });
            }
        });
    }
})(jQuery);

我的控制器返回的我想要什么选择列表。

My controller returns a select list of what i want.

public ActionResult FilterFieldOffices(int companyId = 0)
    {
        IList<FieldOffice> list = _fodp.GetFieldOfficesForCompany(companyId);

        var returnList = new SelectList(list, "Id", "FacilityName");

        return Json(returnList);
    }

我知道.change功能被放置警示射击,我知道我的控制器功能是通过所谓的断点,这样范围缩小到loadselect功能,但是我不能缩小它了,因为没有将警报负载选择功能内火。我没有用jQuery足够的经验,形成对什么是错的意见。因此,即时通讯,询问是否任何人有什么即时试图做任何的成功,或者如果你可以看到什么毛病code。
而有人问之前,是的,我已经检查了支架。

I know the .change function is firing by placing alerts, and i know that my controller function is being called through breakpoints, so that narrows it down to the loadselect function, however i cant narrow it down anymore, since no alerts will fire within the load select function. I dont have enough experience with JQuery to form an opinion on whats wrong. So im asking if anyone has had any success on what im trying to do, or if you can see something wrong with the code. And before anyone asks, yes i have checked the brackets.

修改的忘了提,我检查了Firefox的错误控制台,我得到的错误:

edit Forgot to mention that i checked the error console for firefox and i get the error:

$(ddl).loadSelect is not a function.

修改的发现,提到当你有两个相互矛盾的框架,我描述的错误出现的资源。所以,我把jQuery.noConflict();它仍然无法正常工作。

edit Found a resource that mentions that the error i described arises when you have two conflicting frameworks. So i put in jQuery.noConflict(); and it still does not work.

推荐答案

非常的bizzare,但我想出了,我可以突破点的解决方案。

Very bizzare, but i came up with a solution that i can break point through.

<script type="text/javascript">

        $(function () {
            $('#cid').change(function () {
                var coid = $(this).val();
                $.post("/TimeTracking/FilterFieldOffices", { companyId: coid }, function (data) {
                    $("#foid").loadSelect(data); 
                });
            });
        });

        $(function () {
            $.fn.loadSelect = function (data) {
                return this.each(function () {
                    this.options.length = 0;
                    $.each(data, function (index, itemData) {
                        var option = new Option(itemData.Text, itemData.Value);
                        this.add(option);
                    });
                });
            };
        });

    </script>

所有我要做的就是包裹的wasnt被称为在$(函数(){// code这里})的功能;
不过IM仍然遇到了一些麻烦。我做了一个新的一页吧。

All i had to do was to wrap the function that wasnt being called in a $(function() { //Code here });However im still having some trouble. I made a new page for it.

http://stackoverflow.com/questions/3965283/refreshing-a-dropdownlist-after-elements-have-been-reset

这篇关于使用JQuery更新Html.Dropdownlists的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 05:55