You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

jQuery触发Enter键盘事件失败问题求助

问题:模拟Enter按键无法触发recipal.com搜索框预期效果

场景说明

  • 网站:recipal.com(需登录)
  • 登录信息:邮箱yeti.floats@gmail.com,密码StackOverflow1!
  • 目标页面:登录后访问https://www.recipal.com/recipes/1698297/edit,目标输入框为#search_field

问题现状

脚本已成功给输入框赋值并设置焦点,但模拟Enter按键事件后未触发预期搜索效果。对比模拟事件与真实人工输入事件的控制台输出,发现差异仅在于模拟事件缺少带有isTrusted: true的originalEvent属性,且该属性无法伪造。

使用的jQuery代码

var press = jQuery.Event("keypress");
press.which = 13;
press.keyCode = 13;
press.charCode = 13;
press.key='Enter';
press.bubbles=true;

$('#search_field').attr('tabindex',1);
$('#search_field').focus().trigger('click').val("chicken");
setTimeout(()=>{

    $('#search_field').trigger(press).trigger('keyup');
},1000);

模拟事件控制台输出

{"type": "keypress","timeStamp": 1704464067050,"jQuery34105937047508940263": true,"which": 13,"keyCode": 13,"charCode": 13,"key": "Enter","bubbles": true,"isTrigger": 3,"namespace": "","rnamespace": null,"target": {"jQuery341059370475089402631": {"events": {"focus": [{"type": "focus","origType": "focus","guid": 118,"namespace": false},{"type": "focus","origType": "focus","guid": 117,"namespace": ""}]},"focus": false}},"delegateTarget": {"location": {"ancestorOrigins": {},"href": "https://www.recipal.com/recipes/1230714/edit","origin": "https://www.recipal.com","protocol": "https:","host": "www.recipal.com","hostname": "www.recipal.com","port": "","pathname": "/recipes/1230714/edit","search": "","hash": ""},"jQuery341059370475089402631": {"events": {"mouseup": [{"type": "mouseup","origType": "mouseup","guid": 3,"namespace": ""}],"change": [{"type": "change","origType": "change","guid": 5,"selector": "#faSizeIngred","needsContext": false,"namespace": ""}],"keypress": [{"type": "keypress","origType": "keypress","guid": 6,"selector": "#search_field","needsContext": false,"namespace": ""}],"focusin": [{"type": "focusin","origType": "focus","guid": 7,"selector": "#search_field","needsContext": false,"namespace": ""}],"click": [{"type": "click","origType": "click","guid": 8,"selector": ".btn-addtorecipe:not(.btn-mini)","needsContext": false,"namespace": ""},{"type": "click","origType": "click","guid": 10,"selector": "#pushMacs","needsContext": false,"namespace": ""},{"type": "click","origType": "click","guid": 30,"namespace": "menu1"},{"type": "click","origType": "click","guid": 54,"namespace": "menu3"},{"type": "click","origType": "click","guid": 78,"namespace": "menu5"},{"type": "click","origType": "click","guid": 102,"namespace": "menu7"}],"keyup": [{"type": "keyup","origType": "keyup","guid": 9,"selector": "#faAggredName","needsContext": false,"namespace": ""}]},"focusin": 1}},"currentTarget": {"jQuery341059370475089402631": {"events": {"focus": [{"type": "focus","origType": "focus","guid": 118,"namespace": false},{"type": "focus","origType": "focus","guid": 117,"namespace": ""}]},"focus": false}},"handleObj": {"type": "keypress","origType": "keypress","guid": 6,"selector": "#search_field","needsContext": false,"namespace": ""}}

人工输入事件数据

{"originalEvent": {"isTrusted": true},"type": "keypress","target": {"jQuery341059370475089402631": {"events": {"focus": [{"type": "focus","origType": "focus","guid": 118,"namespace": false},{"type": "focus","origType": "focus","guid": 117,"namespace": ""}]},"focus": false}},"currentTarget": {"jQuery341059370475089402631": {"events": {"focus": [{"type": "focus","origType": "focus","guid": 118,"namespace": false},{"type": "focus","origType": "focus","guid": 117,"namespace": ""}]},"focus": false}},"timeStamp": 24011519.5,"jQuery34105937047508940263": true,"delegateTarget": {"location": {"ancestorOrigins": {},"href": "https://www.recipal.com/recipes/1230714/edit","origin": "https://www.recipal.com","protocol": "https:","host": "www.recipal.com","hostname": "www.recipal.com","port": "","pathname": "/recipes/1230714/edit","search": "","hash": ""},"jQuery341059370475089402631": {"events": {"mouseup": [{"type": "mouseup","origType": "mouseup","guid": 3,"namespace": ""}],"change": [{"type": "change","origType": "change","guid": 5,"selector": "#faSizeIngred","needsContext": false,"namespace": ""}],"keypress": [{"type": "keypress","origType": "keypress","guid": 6,"selector": "#search_field","needsContext": false,"namespace": ""}],"focusin": [{"type": "focusin","origType": "focus","guid": 7,"selector": "#search_field","needsContext": false,"namespace": ""}],"click": [{"type": "click","origType": "click","guid": 8,"selector": ".btn-addtorecipe:not(.btn-mini)","needsContext": false,"namespace": ""},{"type": "click","origType": "click","guid": 10,"selector": "#pushMacs","needsContext": false,"namespace": ""},{"type": "click","origType": "click","guid": 30,"namespace": "menu1"},{"type": "click","origType": "click","guid": 54,"namespace": "menu3"},{"type": "click","origType": "click","guid": 78,"namespace": "menu5"},{"type": "click","origType": "click","guid": 102,"namespace": "menu7"}],"keyup": [{"type": "keyup","origType": "keyup","guid": 9,"selector": "#faAggredName","needsContext": false,"namespace": ""}]},"focusin": 1}},"handleObj": {"type": "keypress","origType": "keypress","guid": 6,"selector": "#search_field","needsContext": false,"namespace": ""}}

请求

求解决思路,如何绕过isTrusted限制触发Enter按键的预期效果?

内容的提问来源于stack exchange,提问作者macheteYeti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 01:40:31