如何对动态加载的标签绑定事件

动态加载的标签,事件不能用bind,需要使用on 来绑定注册事件

后台方法

 /// <summary>
        /// 获取系统花色尺码信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public virtual QueryListResponseDTO<object> GetColorAndSizeInfo([FromBody]JObject data)
        {
            var retDTO = new QueryListResponseDTO<object>();
            try
            {
                var sDTOs = ComAS.GetColorAndSizeInfo();
                retDTO.data.AddRange(sDTOs);
                return retDTO;
            }
            catch (Exception ex)
            {
                retDTO.success = false;
                retDTO.message = ex.Message;
                return retDTO;
            }
        }
        2. js调用后台方法
          function loadColorAndSize() {
                var url = getSiteRootPath() + "/api/Common/GetColorAndSizeInfo";
                $.ajax({
                    url: url,
                    type: "POST",
                    data: { "loadAll": "1" },
                    //beforeSend: function () {
                    //    $.common.showMask("正在获取花色和尺码信息…");
                    //},
                    error: function () {
                        //  $("#save").removeAttr("disabled");
                        $.common.hideMask();
                    },
                    success: function (result) {
                        //  $("#save").removeAttr("disabled");
                        $.common.hideMask();
                        if (result) {
                            if (result.success) {
                                var rlt = result.data;
                                if (rlt != null && rlt.length >= 5) {
                                    $("#txtIsAutoItemNo").val(rlt[4]);
                                    sizeAry = rlt[1];
                                    colorArg = rlt[3];
                                    //默认加载第一个尺码组的信息
                                    loadSizeGroup(rlt[0]);
                                    loadSize(rlt[0][0].size_group_id);

                                    loadColorGroup(rlt[2]);
                                    loadColor(rlt[2][0].color_group_id);
                                }
                            } else {
                                $.common.info(result.message);
                            }
                        }
                    }
                });
            }

            3. 事件绑定
              $(document).on('click', '#size .sizeBoxTop .sizeBlock div', function () {
                if ($('#selApproveStatue').val() == "3" || $('#selApproveStatue').val() == "4") return;
                $("#size .sizeBoxTop .sizeBlock .sizeBlockSelect").attr("class", "sizeBlockUnSelect");
                if ($(this).hasClass("sizeBlockUnSelect")) $(this).attr("class", "sizeBlockSelect");
                var groupID = $(this).attr("data-code").trim();
                if (groupID != null && groupID != undefined && groupID.trim().length > 0) {
                    if (oldSizeGrupID != null && oldSizeGrupID.trim().length > 0 && oldSizeGrupID.trim() != groupID) {
                        $.common.confirm("切换尺码组时,将会按新的规则生成子码,之前子码将会被清空?", function (result) {
                            if (result) {
                                oldSizeGrupID = groupID;
                                loadSize(groupID);
                                $(".subTable tbody").empty();
                                return false;
                            }
                        });
                    }
                    else
                       loadSize(groupID);
                }
            });

            $('#supplies').supplies({ rows: 10, page: 1, loadAll: false, search: true, eventMode: 1, afterSelect: function (no) { GetSupFlag(no) } });

--参数传递回调函数,判断参数是否是一个函数

 if (options.afterSelect && $.isFunction(options.afterSelect)) {
                    options.afterSelect(b_no);
                }

注意:有时候,在苹果手机上,绑定的事件不生效,可以对选择器增加一个css

      .sizeBlockItem {
            cursor:pointer;
        }

sizeBlockItem  对应选择器的样式 classid

猜你喜欢

转载自blog.csdn.net/shuang7924/article/details/82225524