简体   繁体   English

jQuery更改功能似乎可以工作,但是什么也没做

[英]Jquery change function seems to work but doesn't do anything

I have 2 radio buttons with jquery code that rebuilds a dropdownlist with just the options that they choose. 我有2个带有jQuery代码的单选按钮,这些按钮仅使用它们选择的选项来重建下拉列表。 I have the code below and it goes to the proper method and the method to build the select list works properly and after the method is finished, the dropdownlist items don't change at all. 我有下面的代码,它转到正确的方法,并且用于构建选择列表的方法可以正常工作,并且在该方法完成后,下拉列表项完全不变。 Everything seems to be correct but it just doesn't work. 一切似乎都是正确的,但那是行不通的。 Does anyone have any ideas? 有人有什么想法吗?

View 视图

$('#singleSpacingRadio').change(function () {
        var url = "/Home/NumberOfPagesList?id=" + $('#singleSpacingRadio').val();

        $.getJSON(url, function (data) {
            var items = "<option>--Test--</option>";
            $.each(data, function (i, numberOfPages) {
                items += "<option value='" + numberOfPages.Value + "'>" + numberOfPages.Text + "</option>";
            });
            $("#numberOfPagesList").html(items);
        });
    });

    $('#doubleSpacingRadio').change(function () {
        var url = "/Home/NumberOfPagesList?id=" + $('#doubleSpacingRadio').val();

        $.getJSON(url, function (data) {
            var items = "<option>--Test--</option>";
            $.each(data, function (i, numberOfPages) {
                items += "<option value='" + numberOfPages.Value + "'>" + numberOfPages.Text + "</option>";
            });
            $("#numberOfPagesList").html(items);
        });
    });

<div class="row">
    @Html.LabelFor(model => model.Spacing, "Spacing:")
    @Html.RadioButtonFor(model => model.Spacing, "Single", new { id = "singleSpacingRadio" }) Single
    @Html.RadioButtonFor(model => model.Spacing, "Double", new { id = "doubleSpacingRadio" }) Double
</div>

Controller 调节器

public ActionResult NumberOfPagesList(string id)
    {
        var numberOfPagesList = from n in NumberOfPages.GetNumberOfPages()
                          where n.Spacing == id
                          select n;

        if (HttpContext.Request.IsAjaxRequest())
        {
            return Json(new SelectList(numberOfPagesList.ToArray(), "numberOfPagesValue", "numberOfPagesName"), JsonRequestBehavior.AllowGet);
        }
        else
        {
            return RedirectToAction("Contact");
        }
    }

public class NumberOfPages
{
    public string Name { get; set; }
    public string Value { get; set; }
    public string Spacing { get; set; }

    public static IQueryable<NumberOfPages> GetNumberOfPages()
    {
        return new List<NumberOfPages>
        {
            new NumberOfPages
            {
                Name = "1 Page (Approx. 550 Words)",
                Value = "1",
                Spacing = "Single"
            },
            new NumberOfPages
            {
                Name = "1 Page (Approx. 275 Words)",
                Value = "1",
                Spacing = "Double"
            },
            new NumberOfPages
            {
                Name = "50 Pages (Approx. 13750 Words)",
                Value = "50",
                Spacing = "Double"
            }
        }.AsQueryable();
    }

Change the html to 将html更改为

@Html.RadioButtonFor(m => m.Spacing, "Single", new { @class = "spacing", id="single" })
<label for="single">Single</label>
@Html.RadioButtonFor(m => m.Spacing, "Double", new { @class = "spacing", id="double" })
<label for="double">Double</label>

and the script to 和脚本

$('.spacing').click(function() {
  var select = $("#numberOfPagesList");
  select.empty().append($('<option></option>').val('').text('--Test--'));
  var url = '@Url.Action("NumberOfPagesList", "Home")';
  $.getJSON(url, { id: $(this).val() }, function (data) {
    $.each(data, function (i, item) {
      select.append($('<option></option>').val(item.Value).text(item.Name));
    });
  });
});

Note also you do not need to return convert your collection to a SelectList and only the following is required 另请注意,您不需要将集合转换为SelectList返回,并且仅需要以下内容

return Json(numberOfPagesList, JsonRequestBehavior.AllowGet);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM