【问题标题】:My function is not returning all checked Values我的函数没有返回所有选中的值
【发布时间】:2013-09-13 14:26:11
【问题描述】:

我正在填充 Ajax 调用的复选框列表,并且我试图获取哪些复选框已被选中并且我只获得第一个值。如果我做错了,请帮助我。

我的 HTML:

<%@ Control Language="C#"Inherits="System.Web.Mvc.ViewUserControl<EmsAdmin.Models.User>" %>
<%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors andtryagain.") %>
<% using (Html.BeginForm(null, null, FormMethod.Post, new { name = 
"VerficationForm", id = "VerficationForm" }))
 {%>
<%= Html.AntiForgeryToken() %>
<%: Model.Id %>
<h2>Go For All / Individual Brands</h2>
<p>
<input type="checkbox" id="checkBoxAllSelect" class="checkBoxAllSelect" value="All" />
All
<br />
<input type="checkbox" id="checkBox1Individual" class="checkBox1Individual" 
 value="Individual" />
 Individual
<br/>
 <input type="button" value="GetBrands" class="getBrands" />
</p>
<hr />
<h2>Multi Select Brands If Required:</h2>
 <div id="checkboxes" class ="divNewBrandsList">
 </div>
 <hr />
<h2>Countires For Each Brand:</h2>
<div id="checkboxescountries" class="divNewCountriesList">
 </div>
 <hr />
 <% } %>

脚本:

 <script type="text/javascript">
 $('.getBrands').hide();
function myBrandsfunction() {
    var selectedBrands = "";
    var manu = "";
    var input = "";
    $("#checkboxes input:checked").each(function () {
        manu = $(this).val();
        alert("Brand value:" + manu); // Here I am getting only one Selected checkbox but not all.
        selectedBrands = selectedBrands + "," + manu;
        alert("Selected brands:" + selectedBrands);
        var productInput = "";
        var myUrl = "/Countries/GetCountiresForManufacturer/" + selected + "/" + input;
        $.ajax({
            url: myUrl,
            type: 'get',
            success: function (data) {
                productInput = data;
                $(".divNewCountriesList").html(productInput);
            }
        });
    });
}

控制器:

    /// <summary>
    /// GetBrands for Organisation
    /// </summary>
    /// <returns></returns>
    [Authorize(Roles = "Administrator")]
    [AcceptVerbs(HttpVerbs.Get)]
    public string GetBrandsForOrganisation(string organisation)
    {
        var sb = new StringBuilder();
       // var brandsList = new List<String>();
        if (organisation == null)
        {
            return "";
        }
        else
        {
            var corporation = _corporationRepository.GetCorporationByName(organisation);
            if (corporation != null)
            {
                // Get Manufactuerers 
                var brands = _corporationManufacturerRepository.GetAllManufacturersForCorporation(corporation.Id);

                sb.Append("<div id = \"" + organisation + "\">");
                foreach (var brand in brands)
                {
                    sb.Append("<input id =\"" + brand.Manufacturer.Id +
                              "\" type=\"checkbox\" class=\"checkboxBrand\" value=\"" +
                              brand.Manufacturer.Description + "\"/>" + brand.Manufacturer.Description);
                    sb.Append("<br />");
                }
                sb.Append("<br />");
                sb.Append("</div>");
            }

        }
        sb.Append("<input type=\"button\" value=\"GetCountries\"  onclick = \"myBrandsfunction()\"class=\"brands\"/>");

        return sb.ToString();
    }

【问题讨论】:

  • 您能否尝试将您的代码范围缩小到发生错误的确切位置,人们不太可能筛选所有代码来发现错误。
  • @RichardDalton 已删除

标签: javascript asp.net-mvc jquery


【解决方案1】:

我相信您遇到的问题是它正在更新所有内容,但只有最后一个还活着,因为您不断用您最后的回复替换 .divNewCountriesList 的 html。您可能需要构建一个数组/长字符串:

var checkedItems = $("#checkboxes input:checked");

var productInputList = []; 

checkedItems.each(function (index,element) {
    manu = $(this).val();
    alert("Brand value:" + manu); // Here I am getting only one Selected checkbox but not all.
    selectedBrands = selectedBrands + "," + manu;
    alert("Selected brands:" + selectedBrands);
    var productInput = "";
    var myUrl = "/Countries/GetCountiresForManufacturer/" + selected + "/" + input;


    $.ajax({
        url: myUrl,
        type: 'get',
        success: function (data) {
            productInput = data;
            productInputList.push(productInput);
            if (index == checkedItems.length - 1) {
                //Reached end of list... populate
                $(".divNewCountriesList").html(productInputList.join(' '));
            }
        }
    });
});

或者,您可以按照here 的建议使用 deferred 来避免混乱的索引检查/很棒。

【讨论】:

  • @J Torres 感谢您的帖子。但我没有得到价值。我期待的是,如果checkedItems 是Dove,当然我的manu 应该是Dove 然后选择= ,Dove 和下一个Sure manu = Sure 和Selected = ,Dove,Sure 这是我期待的结果,但现在我只是得到Dove 和我的url 也是空的。
  • 我不明白你的评论或“鸽子”。
  • @J Torres 我说比如 Dove。
【解决方案2】:

问题可能是:

你有一个:

$("#checkboxes input:checked") 

....但是您的 div 看起来像...

<div id="checkboxes" class ="divNewBrandsList">

这是一个 id 问题...尝试更改复选框的 id。

======================更新======================

这似乎也很奇怪:

 //This line is storing just one value and its normal
 manu = $(this).val(); 

但重要的部分是 foreach 循环中的 .ajax 调用,只是,为什么? ... url中的输入get值也是空的。

这里:

var myUrl = "/Countries/GetCountiresForManufacturer/" + selected + "/" + input;

【讨论】:

  • 一开始我也认为这是问题所在,但我相信我们没有看到将GetBrandsForOrganisation 的输出注入#checkboxes 的代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-20
  • 2017-06-04
  • 1970-01-01
  • 1970-01-01
  • 2017-06-25
  • 2014-02-27
相关资源
最近更新 更多