Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我的函数没有返回所有选中的值_Javascript_Asp.net Mvc_Jquery - Fatal编程技术网

Javascript 我的函数没有返回所有选中的值

Javascript 我的函数没有返回所有选中的值,javascript,asp.net-mvc,jquery,Javascript,Asp.net Mvc,Jquery,我在Ajax调用中填充复选框列表,并尝试获取选中的复选框,并且只获取第一个值。如果我做错了什么,请帮助我 我的Html: <%@ Control Language="C#"Inherits="System.Web.Mvc.ViewUserControl<EmsAdmin.Models.User>" %> <%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors andtr

我在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);
            }
        });
    });
}

$('.getBrands').hide();
函数myBrandsfunction(){
var selectedBrands=“”;
var manu=“”;
var输入=”;
$(“#复选框输入:选中”)。每个(函数(){
manu=$(this.val();
警告(“品牌价值:“+manu”);//这里我只得到一个选中的复选框,但不是全部。
selectedBrands=selectedBrands+“,”+manu;
警报(“选定品牌:+选定品牌”);
var productInput=“”;
var myUrl=“/Countries/getcountriersformanufacturer/”+所选+“/”+输入;
$.ajax({
url:myUrl,
键入:“get”,
成功:功能(数据){
productInput=数据;
$(“.divNewCountriesList”).html(产品输入);
}
});
});
}

控制器:

    /// <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();
    }
//
///获取组织的品牌
/// 
/// 
[授权(角色=“管理员”)]
[接受动词(HttpVerbs.Get)]
公共字符串GetBrandsForOrganization(字符串组织)
{
var sb=新的StringBuilder();
//var brandsList=新列表();
如果(组织==null)
{
返回“”;
}
其他的
{
var corporation=\u corporationRepository.GetCorporationByName(组织);
if(corporation!=null)
{
//找制造商
var brands=\u corporationManufacturerRepository.GetAllManufacturersForCorporation(corporation.Id);
某人加上(“”);
foreach(品牌中的var品牌)
{
sb.附加(“+品牌.制造商.说明);
sb.追加(“
”); } sb.追加(“
”); 某人加上(“”); } } 某人加上(“”); 使某人返回字符串(); }
我相信您遇到的问题是,它正在更新所有内容,但只有最后一个仍然有效,因为您不断地用最后一个响应替换
.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(' '));
            }
        }
    });
});
或者,您可以按照建议使用“延迟”来避免混乱的索引检查/非常棒。

可能是因为:

您有:

$("#checkboxes input:checked") 
但是你的div看起来像

<div id="checkboxes" class ="divNewBrandsList">
但重要的部分是foreach循环中的.ajax调用,只是,为什么。。。url中的输入get值也是空的

在这里:

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

你能试着把你的代码缩小到错误发生的确切位置吗?人们不太可能通过筛选所有的代码来发现错误。@RichardDalton deletedI起初认为这也是问题所在,但我相信我们没有看到将
getbrandsforororganization
的输出注入
#复选框的代码。谢谢你的帖子。但我没有得到价值。我所期待的是,如果checkedItems是Dove,那么我的manu肯定应该是Dove,然后selected=,Dove和next Sure manu=Sure和selected=,Dove,当然这就是我所期待的结果,但现在我只是得到了Dove和我的url都为null。我不理解你的评论或“Dove”。@J Torres我说的是Dove。
var myUrl = "/Countries/GetCountiresForManufacturer/" + selected + "/" + input;