
问题原因amis 6.13.0 的删除按钮设置clearable为true显示的x按钮触发 focus 事件——源码里clearValue只对click做了preventDefault stopPropagation但没拦 mousedown 引起的原生聚焦所以焦点必然先落到 Select 根节点tabIndex0派发 focus。平常的使用没有问题如果在focus事件中使用setValue给当前select组件赋值就会出现点击x按钮期望是清除select的值反而变成了focus事件送过来的值不符合需求。解决方案1.写个全局补丁文件多加一个自定义参数这个参数为true时阻止触发focus事件彻底解决优点遇到类似的问题只在补丁文件修改不用一个个文件地排查。缺点点击x按钮触发focus事件是某些业务场景需要的原来的代码有其存在的价值全局修改错误影响范围广。2.单个json文件针对性修改优点只在当前页面生效错误影响范围小。缺点遇到类似的问题只能一个个找出来修改。3.使用setTimeout延时500ms再强制触发clear事件优点逻辑简单但最好做上述2个方案的兜底选择。缺点如果有ajax事件setValue赋值用时就不确定了延时500ms可能还不够。核心代码片段// 核心代码 var active document.activeElement;\r\nvar root active active.closest ? active.closest(.cxd-Select) : null; if (root root.querySelector(a.cxd-Select-clear:hover)) { event.stopPropagation(); return; }demo代码示例注意只是提供思路需要根据自己的实际情况处理项目代码。{ type: select, id: selectValue_id, name: selectValue, label: 测试selectValue, source: $arr, onEvent: { focus: { actions: [ { script: // 点击清除按钮(cxd-Select-clear)时不再触发 focus 联动,只保留按钮自带的清除行为。\r\n// 原理: mousedown 会让焦点先落到 Select 根节点(tabIndex0)从而派发 focus 事件,\r\n// 此刻鼠标仍悬停在清除按钮上,因此用 :hover 命中即可判定本次 focus 来自清除按钮,\r\n// stopPropagation 短路本事件后续的 ajax/setValue 动作;\r\n// 正常点击选择器(非清除按钮)时无 :hover 命中,原有行为保持不变。\r\nvar active document.activeElement;\r\nvar root active active.closest ? active.closest(.cxd-Select) : null;\r\nconsole.log(root, 点击清除按钮(cxd-Select-clear)时不再触发 focus 联动,只保留按钮自带的清除行为。)\r\nif (root root.querySelector(a.cxd-Select-clear:hover)) {\r\n event.stopPropagation();\r\n return;\r\n}, actionType: custom }, { api: { url: /api/xxx, data: { aaa: 111 }, method: get }, options: {}, outputVar: aaaResult, actionType: ajax }, { args: { value: { selectValue: ${aaaResult} } }, actionType: setValue, componentId: form-1 } ] }, change: { actions: [ { args: {}, script: if (event.data.value) {\r\n return;\r\n}\r\nsetTimeout(() {\r\n doAction({\r\n componentId: selectValue_id,\r\n actionType: clear\r\n });\r\n}, 500);, actionType: custom } ] } }, multiple: false, clearable: true, joinValues: false }