1

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();
    }
4

2 に答える 2

0

問題は次のとおりです。

あなたが持っている:

$("#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;
于 2013-09-13T14:52:47.260 に答える