我的函数没有返回所有选中的值
My function is not returning all checked Values
我正在填充Ajax调用上的复选框列表,我正在尝试获取选中了哪些复选框,但我只得到了第一个值。如果我做错了什么,请帮助我。
我的网页:
<%@ 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();
}
我相信
您遇到的问题是它正在更新所有内容,但只有最后一个是活动的,因为您不断将.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">
这是身份问题...尝试更改复选框的 ID。
====
=================更新==============================这似乎也很奇怪:
//This line is storing just one value and its normal
manu = $(this).val();
但重要的部分是foreach循环中的.ajax调用,只是,为什么?? ...URL 中的输入 GET 值也是空的。
这里:
var myUrl = "/Countries/GetCountiresForManufacturer/" + selected + "/" + input;
相关文章:
- 使用返回函数sinde.attr()jquery元素
- 从自执行函数返回函数的Javascript性能命中率
- Node Express Handlebars帮助程序未返回函数的结果
- 未在Firefox中执行PageMethod的返回函数
- 对返回函数的函数感到困惑
- 从承诺返回不返回函数会导致警告
- 从函数返回函数而不调用返回的函数
- Javascript,闭包中的返回函数如何与外部函数连接
- 为什么Coderbyte.com's的Javascript模板喜欢返回函数的原始参数
- 对象函数返回函数而不是值
- 从外部函数(数组)了解返回函数(x)
- 从Javascript类对象返回函数
- 调用Typescript setter don't返回函数,尽管关联的getter可以工作
- 使用依赖注入在 JavaScript 中返回函数
- 通过单击JSP和javascript加载两个返回函数
- 需要说明:无法理解返回函数的javascript
- 简单的onClick返回函数不起作用
- 为什么这个闭包返回函数
- JS函数返回函数供以后使用-未定义参数
- Coffeescription类中的方法返回函数而不是字符串