如何對動態加載的標籤綁定事件

動態加載的標籤,事件不能用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);
                }
            });

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章