jQuery Sortable拖拽Grid中getMapping()数组Ajax发送报HTMLElement错误
基于jQuery Sortable实现支持拖拽调整位置的HTML Grid布局,用户可自由修改网格内div的排列顺序;点击保存按钮时调用getMapping()函数,通过DOM id遍历收集所有div的排列顺序数据。开发时在map遍历逻辑中加入if/else判断,用于识别区分头部容器和普通内容块;通过console.log调试时无报错,可输出结构符合预期的数组数据。
尝试将getMapping()生成的数组通过Ajax传输到后端时触发报错,异常仅在发起Ajax请求的环节出现,数组本身的生成过程完全正常。
具体报错信息:
Uncaught TypeError: 'click' called on an object that does not implement interface HTMLElement.
getMapping()函数中通过jQuery的.map()方法收集数据后,直接返回了jQuery包装的类数组对象,而非原生JavaScript数组。
当jQuery Ajax模块处理传入的请求参数时,会将这个jQuery包装对象误识别为DOM元素集合,在遍历解析参数的过程中触发了错误的方法调用,最终抛出上述类型错误。
交互逻辑JS
$(".dropzone").sortable({ connectWith: ".dropzone", update: function(event, ui) { showResult(); }, placeholder: "dashed" }); var headerCount = 1; function addHeader() { $(".dropzone").prepend( $("#prefab-header") .clone() .attr("id", "header-" + headerCount) .html("Header " + headerCount++) ); } function showResult() { var arr = $(".dropzone > div").map(function() { return this.id; }); $("#result").html(""); $("#result").append(arr.get().join(", ")); } function getMapping() { var sort = 1; var data = $(".dropzone > div").map(function() { var array = []; var add = {}; if (this.id.includes("head-")) { add["data"] = 0; add["name"] = this.innerText; add["sort"] = sort++; array.push(add); } else { add["data"] = this.id; add["sort"] = sort++; array.push(add); } return array; }); // 问题点:此处返回的是jQuery包装对象,不是原生数组 return data; } function sendAjax() { var data = getMapping(); console.log(data); $.ajax({ url: "cal-admin.php", type: "post", data: { data: data }, success: function (response) {} }); }
样式CSS
.grid-view { margin-top: 50px; width: 700px; background-color: #f5f2f2; padding: 50px; min-height: 350px; } .grid-trash { margin-top: 20px; width: 700px; background-color: #f5f2f2; color: #999; padding: 10px; height: 50px; padding: 10px; border: 2px dashed #999; text-align: center; } .grid-trash-hover { background-color: #f34541; color: #fff; border: 2px dashed #fff; } .grid-result { margin-top: 20px; width: 700px; background-color: #f5f2f2; padding: 10px; height: 100px; } .grid-users { background: #f5f5f5; padding-bottom: 20px; padding-top: 20px; padding-right: 0px; padding-left: 0px; cursor: move; border: #000 1px solid; text-align: center; font-weight: bold; } .grid-header { background: #34bdeb; padding-bottom: 5px; padding-top: 5px; padding-right: 0px; padding-left: 0px; cursor: move; border: #000 1px solid; text-align: center; font-weight: bold; } .dashed { border: 2px dashed #999; background: #ede8e8; height: 75px; }
页面结构HTML
<!-- 本地引入jQuery、Bootstrap、jQueryUI依赖 --> <div class="container "> <div class="row justify-content-center align-items-center"> <div class="grid-view"> <div style="padding-bottom: 10px"> <button type="button" class="btn btn-primary" onclick="addHeader();">Add header</button> <button type="button" class="btn btn-info" onclick="showResult();">Display DIV id order</button> </div> <div id="dropzone" class="dropzone"> <div id="team-1" class="grid-users">TEAM 1</div> <div id="team-2" class="grid-users">TEAM 2</div> </div> <div style="margin-top: 20px"> <button type="button" class="btn btn-info" onclick="sendAjax();">Send Ajax</button> </div> </div> </div> </div> <div class="container"> <div class="row justify-content-center align-items-center"> <div class="grid-result"> <span><b>Console log:</b></span> <div id="result" style="height: 50px; padding: 10px; border: 2px dashed #999"></div> </div> </div> </div> <div style="display:none"> <div id="prefab-header" class="drag grid-header"></div> </div>
将getMapping()函数的返回值从直接返回jQuery对象,调整为返回转换后的原生数组,即修改代码为:
function getMapping() { var sort = 1; var data = $(".dropzone > div").map(function() { // 原有遍历逻辑保持不变 }); // 调用.get()将jQuery包装对象转为原生数组 return data.get(); }
补充说明:jQuery的.map()方法会自动将回调返回的嵌套数组拍平,经过.get()转换后得到的是标准一维对象数组,结构完全满足后端接口的参数接收要求;Ajax模块识别到原生数组类型的参数后,就不会再误判为DOM元素集合触发类型错误。
内容的提问来源于stack exchange,提问作者user3877838

