下拉列表没有填写Jquery,Ajax(dropdown list does not fill in Jquery, Ajax)

编程入门 行业动态 更新时间:2024-10-22 08:15:15
下拉列表没有填写Jquery,Ajax(dropdown list does not fill in Jquery, Ajax)

我想通过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

更多推荐

本文发布于:2023-04-28 02:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330289.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   Jquery   Ajax   fill   list

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!