我想通过jQuery填充一个drodown-list,Ajax,在第一个下拉列表中可见= false当用户单击一个按钮时它将出现并同时必须填充数据,
一切都很好,但数据没有填充下拉列表我利用这个链接来制作这个代码: link
我在javascript中给每个警报放了一个警告,所以我发现它传递了数据,因为它迭代了10次(根据我在db中的数据),但为什么只是它没有显示数据????? 这是我的下拉列表
<input type="button" onclick="edit(@item.Barcode);" value="خرید" class="btn btn-success btn-sm"> <div class="pull-left" style="display:none" id="pnlSize"> <label class="control-label">سایز:</label> <div class="product-quantity" id="drpSize"> @Html.DropDownListFor(m => m.Size, new SelectList(Enumerable.Empty <SelectListItem>(), "ID", "Size"), "انتخاب همه", new { @class = "form-control" }) </div> </div>这是我的Javascript:
< script > function edit(Barcode) { $.ajax({ url: '@Url.Action("selectedItemChanged")', type: 'GET', data: { barcode: Barcode }, success: function(result) { // when the AJAX succeeds refresh the dropdown list with // the JSON values returned from the controller action var selectedDeviceModel = $('#Size'); selectedDeviceModel.empty(); $.each(result, function(index, item) { selectedDeviceModel.append( $('<option/>', { value: item.value, text: item.text }) ); }); } }); $('#pnlSize').show('fast'); var val = $('#drpSize :selected').text(); if (val != 'انتخاب همه' && val != null) { location.href = '@Url.Action("AddToCart", "ShoppingCart")?Barcode=' + Barcode + '&size=' + $('#drpSize :selected').text();; //window.location.href = url.replace('Barcode', Barcode); } } < /script>这是我的控制器代码:
public JsonResult selectedItemChanged(string barcode) { AdidasWebsiteEntities context = new AdidasWebsiteEntities(); var itemsList = context.tblSiteSizes.Where(item => item.Barcode.Substring(0, item.Barcode.Length - 3) == barcode.Substring(0, barcode.Length - 3)).ToList(); return Json(new[] { new { value=itemsList[0].ID, text=itemsList[0].Size } }); }希望有一点时间在此,我厌倦了测试和网上冲浪
I wanna to fill a drodown-list through jQuery, Ajax, at first dropdown list is visible =false when user click a button it will be appeared and at the same time have to be filled with data,
every thing is good, but data does not fill the dropdown list I utilize this link for making this code: link
I put an alert in the each in javascript, and so I found that it pass the data, as it iterated 10 times(according my data in db) but why just it does not show the data????? this is my dropdown list
<input type="button" onclick="edit(@item.Barcode);" value="خرید" class="btn btn-success btn-sm"> <div class="pull-left" style="display:none" id="pnlSize"> <label class="control-label">سایز:</label> <div class="product-quantity" id="drpSize"> @Html.DropDownListFor(m => m.Size, new SelectList(Enumerable.Empty <SelectListItem>(), "ID", "Size"), "انتخاب همه", new { @class = "form-control" }) </div> </div>this is my Javascript:
< script > function edit(Barcode) { $.ajax({ url: '@Url.Action("selectedItemChanged")', type: 'GET', data: { barcode: Barcode }, success: function(result) { // when the AJAX succeeds refresh the dropdown list with // the JSON values returned from the controller action var selectedDeviceModel = $('#Size'); selectedDeviceModel.empty(); $.each(result, function(index, item) { selectedDeviceModel.append( $('<option/>', { value: item.value, text: item.text }) ); }); } }); $('#pnlSize').show('fast'); var val = $('#drpSize :selected').text(); if (val != 'انتخاب همه' && val != null) { location.href = '@Url.Action("AddToCart", "ShoppingCart")?Barcode=' + Barcode + '&size=' + $('#drpSize :selected').text();; //window.location.href = url.replace('Barcode', Barcode); } } < /script>this is my controller code:
public JsonResult selectedItemChanged(string barcode) { AdidasWebsiteEntities context = new AdidasWebsiteEntities(); var itemsList = context.tblSiteSizes.Where(item => item.Barcode.Substring(0, item.Barcode.Length - 3) == barcode.Substring(0, barcode.Length - 3)).ToList(); return Json(new[] { new { value=itemsList[0].ID, text=itemsList[0].Size } }); }hope one kindly take some time on this, I become tired of testing and surfing the net
最满意答案
如果它可能发生在其他人身上,上面代码的问题是下拉列表的名称(大小)和我的意思(大小)项目的名称是相同的,
一旦我更改了下拉列表的名称,它就得到了修复
In case it may happen for someone else, the problem with the above code was that the name of dropdown list (Size) and the name of its items I mean (size) was the same,
once I changed the name of Dropdown list it got fixed
更多推荐
发布评论