代码之家  ›  专栏  ›  技术社区  ›  SimoNR

获取父类中隐藏字段的值和复选框选中的值

  •  0
  • SimoNR  · 技术社区  · 6 年前

        <div class="EmbPosWrap">
        <input class="hidden-field" id="CartItemId" name="hiddenfield" value="167" type="hidden"/>
        <input class="hidden-field" id="StoreId" name="hiddenfield" value="1" type="hidden"/>
        <input class="hidden-field" id="CustomerId" name="hiddenfield" value="1" type="hidden"/>
        <input class="hidden-field" id="ItemID" name="hiddenfield" value="11976" type="hidden"/>
        <div class="EmbPosBx">      
            <input type="checkbox" name="embellishmentcart" value="1" />
            <input type="checkbox" name="embellishmentcart" value="2" />
            <input type="checkbox" name="embellishmentcart" value="5" />
            <input type="checkbox" name="embellishmentcart" value="6" />
        </div>
    </div>
    

    但是HTML可以是这样的,有两个独立的项

        <div class="EmbPosWrap">
        <input class="hidden-field" id="CartItemId" name="hiddenfield" value="167" type="hidden"/>
        <input class="hidden-field" id="StoreId" name="hiddenfield" value="1" type="hidden"/>
        <input class="hidden-field" id="CustomerId" name="hiddenfield" value="1" type="hidden"/>
        <input class="hidden-field" id="ItemID" name="hiddenfield" value="11976" type="hidden"/>
        <div class="EmbPosBx">          
            <input type="checkbox" name="embellishmentcart" value="1" />    
            <input type="checkbox" name="embellishmentcart" value="2" />    
            <input type="checkbox" name="embellishmentcart" value="5" />
            <input type="checkbox" name="embellishmentcart" value="6" />
        </div>
    </div>
    
    <div class="EmbPosWrap">
        <input class="hidden-field" id="CartItemId" name="hiddenfield" value="168" type="hidden"/>
        <input class="hidden-field" id="StoreId" name="hiddenfield" value="1" type="hidden"/>
        <input class="hidden-field" id="CustomerId" name="hiddenfield" value="1" type="hidden"/>
        <input class="hidden-field" id="ItemID" name="hiddenfield" value="1256" type="hidden"/>
        <div class="EmbPosBx">      
            <input type="checkbox" name="embellishmentcart" value="1" />    
            <input type="checkbox" name="embellishmentcart" value="2" />
            <input type="checkbox" name="embellishmentcart" value="3" />    
            <input type="checkbox" name="embellishmentcart" value="4" />
            <input type="checkbox" name="embellishmentcart" value="5" />
            <input type="checkbox" name="embellishmentcart" value="6" />
        </div>
    </div>
    

    滑动分页

    $(function(){    
    var items=$(".EmbPosWrap")
     $.each(items,function (index,item) {      
       alert($(item).attr("value"));       
       var checkboxValues = [];
      $('input[name=embellishmentcart]:checked').map(function () {
        checkboxValues.push($(this).val());
        alert($(item).attr("checkboxValues"));
      });           
    });     
    

    我可以得到一个单一的形式很容易像下面-

     $('#submit').on('click', function () {
      var checkboxValues = [];
      $('input[name=embellishmentcart]:checked').map(function () {
        checkboxValues.push($(this).val());
      });
      var dataRow = {
        'CartItemId': $('#CartItemId').val(),
        'embellishmentcart': checkboxValues,
        'StoreId': $('#StoreId').val(),
        'CustomerId': $('#CustomerId').val(),
        'ItemID': $('#ItemID').val()
      };
      const data = JSON.stringify(dataRow);
      console.log(data);
      $.ajax({
        type: "POST",
        url: '@Url.Action("EmbellishmentOrder", "EmbellishmentCart")',
        dataType: 'json',
        data: dataRow,
        success: function (data) {
          setTimeout(function () {
            window.location.href = data;
          }, 2000);
        }
      });
    });
    

    控制器

       public ActionResult EmbellishmentOrder(EmbellishmentCartDetailModelVM.EmbellishmentCartDetailItemModelVM vm)
    {
      var picId = (int)TempData["RecordId"];
    
      foreach (var item in vm.embellishmentcart)
      {
        EmbellishmentOrderDetailRecord dataModel = new EmbellishmentOrderDetailRecord();
    
        dataModel.CustomerID = vm.CustomerId;
        dataModel.StoreID = vm.StoreId;
        dataModel.CartItemID = vm.CartItemId;
        dataModel.ItemID = vm.ItemID;
        dataModel.PictureId = picId;
        dataModel.EmbellishmentPositionProductDetailID = item;
        _orderDetailService.InsertEmbellishmentOrderDetailRecord(dataModel);
      }
    
      return Json(Url.RouteUrl("ShoppingCart"), JsonRequestBehavior.AllowGet);
    }
    

    namespace Nop.Plugin.Other.ProductEmbellishment.Models.ViewModels
    {
      public partial class EmbellishmentCartDetailModelVM : BaseNopModel
      {
        public EmbellishmentCartDetailModelVM()
        {
          Items = new List<EmbellishmentCartDetailItemModelVM>();
        }
        public bool ShowSku { get; set; }
        public bool ShowProductImages { get; set; }
        public bool IsEditable { get; set; }
        public IList<EmbellishmentCartDetailItemModelVM> Items { get; set; }
        public partial class EmbellishmentCartDetailItemModelVM : BaseNopEntityModel
        {
          public EmbellishmentCartDetailItemModelVM()
          {
            Picture = new PictureModel();
          }
          public PictureModel Picture { get; set; }
    
          public int CustomerId { get; set; }
    
          public int StoreId { get; set; }
    
          public int CartItemId { get; set; }
    
          public int Qty { get; set; }
    
          public string AttributeInfo { get; set; }
    
          public string PictureURL { get; set; }
    
          public string ImageUrl { get; set; }
    
          public string Title { get; set; }
    
          public HttpPostedFileBase File { get; set; }
    
          public int[] embellishmentcart { get; set; }
    
          public int ItemID { get; set; }
    
          public class EmbellishmentPictureModelVM
          {
            public int Id { get; set; }
    
            public string EmbellishmentPositionDescription { get; set; }
    
            public string EmbellishmentPositionCost { get; set; }
    
            public string ImageUrl { get; set; }
    
            public string Title { get; set; }
    
            public string AlternateText { get; set; }
    
            public int ItemID { get; set; }
          }
        }
      }
    }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   user3559349 user3559349    6 年前

    每个 <div class="EmbPosWrap"> 与你的 EmbellishmentCartDetailItemModelVM ,因此需要更改post方法以接受集合

    public ActionResult EmbellishmentOrder (List<EmbellishmentCartDetailModelVM.EmbellishmentCartDetailItemModelVM> vm)
    

    或使用 EmbellishmentCartDetailModelVM 其中包含 修饰CartDetailItemModelVM (the) Items 财产)

    public ActionResult EmbellishmentOrder (EmbellishmentCartDetailModelVM vm)
    

    id 属性,您应该将hdden输入改为使用类名

    <div class="EmbPosWrap">
        <input class="hidden-field" class="CartItemId" name="CartItemId" value="167" type="hidden"/>
        <input class="hidden-field" class="StoreId" name="StoreId" value="1" type="hidden"/>
        ....
        <div class="EmbPosBx">      
             <input type="checkbox" class="embellishmentcart" name="embellishmentcart" value="1" />
             <input type="checkbox" class="embellishmentcart" name="embellishmentcart" value="2" />
             ....
        </div>
     </div>
    

    然后在脚本中需要迭代 <div> 容器和foreach容器构建一个对象并将其添加到一个数组中,然后将该数组发布到方法中

    $('#submit').click(function () {
        var collection = [];
        var containers = $('.EmbPosWrap');
        $.each(containers, function(index, item) {
            var CartItemId = $(this).find('.CartItemId');
            var StoreId = $(this).find('.StoreId');
            ....
            var embellishmentcart = [];
            var checkboxes = $(this).find('.embellishmentcart:checked');
            $.each(checkboxes, function(index, item) {
                embellishmentcart.push($(this).val());
            })
            collection.push({ CartItemId: CartItemId, StoreId: StoreId, .... , embellishmentcart: embellishmentcart });
        })
        $.ajax({
            type: "POST",
            url: '@Url.Action("EmbellishmentOrder", "EmbellishmentCart")',
            dataType: 'json',
            contentType: 'application/json', // add
            data: JSON.stringify(collection), // see note below
            success: function (data) {
                ....
            });
        });
    });
    

    修饰CartDetailModelVM data 选项必须是

    data: JSON.stringify({ Items: collection }),
    
        2
  •  0
  •   halfer    6 年前

    var items=$(".EmbPosWrap") 
    

    <div class="EmbPosWrap">
    

    据我所知,你需要孩子,所以你需要这样做

    var items=$(".EmbPosWrap").children()