js 回车运行

document.onkeydown=function(event){
		var e = event?event:(window.event?window.event:null);
		if (e.keyCode==13){
			searchProduction();
		}
	}


<think>我们正在创建一个HTA插件,要求: 1. 有输入框和输出框 2. 在输入框中输入包含类似“ID=数字”的字符串,按回车键(或者点击按钮)即可触发转换 3. 转换功能:将字符串中的“ID=数字”替换为“dcm=数字” 4. 需要支持按回车运行(即当焦点在输入框时,按回车键触发转换函数) 根据上述要求,我们需要: - 在输入框上添加一个onkeypress事件,检测按下的键是否为回车(Enter)键(键码13) - 转换函数使用正则表达式匹配ID=后面的数字,然后替换为dcm=数字 注意:由于HTA中可以使用VBScript或JScript,这里我们继续使用VBScript。 修改点: 1. 为输入框添加onkeypress事件处理程序,当按下回车键时调用转换函数。 2. 转换函数与之前相同,但为了复用,我们可以让按钮的onclick和输入框的回车事件都调用同一个转换函数。 步骤: 1. 修改输入框元素,添加onkeypress属性:`onkeypress="handleEnterKeyPress"` 2. 在脚本中编写一个处理按键的函数,如果是回车键则调用convertID函数。 3. 注意:VBScript中不支持像JavaScript那样直接使用匿名函数,所以我们用函数包装。 但是,VBScript中事件处理可以直接调用,但判断键码需要用到window.event对象。 具体实现: 在VBScript中,我们可以这样处理按键事件: ```html <input type="text" id="inputText" placeholder="输入包含ID=的字符串" onkeypress="handleEnterKeyPress()"> ``` 然后在VBScript中定义handleEnterKeyPress函数: ```vbscript Sub handleEnterKeyPress() ' 在VBScript中,通过window.event对象获取事件 If window.event.keyCode = 13 Then ' 13是回车键的键码 convertID() End If End Sub ``` 同时,为了确保用户点击按钮也可以触发,我们保留按钮的onclick="convertID()"。 完整代码修改如下: </think>以下是针对您的需求实现的HTA插件解决方案,可通过回车键触发ID到DCM的转换功能: ### HTA插件完整代码 (ID转DCM转换器) ```html <!DOCTYPE html> <html> <head> <title>ID转DCM转换器</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <hta:application id="IDConverter" applicationname="ID转DCM转换器" border="thin" caption="yes" contextmenu="no" showintaskbar="yes" windowstate="normal" scroll="no" > <style> body { font-family: "Microsoft YaHei", Arial, sans-serif; background: linear-gradient(to bottom, #F0F8FF, #E6F7FF); margin: 0; padding: 20px; font-size: 14px; } .container { width: 600px; margin: 20px auto; background: white; padding: 25px; border-radius: 8px; box-shadow: 0 5px 15px rgba(0,0,0,0.1); } h1 { color: #1E90FF; text-align: center; margin-bottom: 25px; text-shadow: 1px 1px 2px rgba(0,0,0,0.1); } .input-group { margin: 18px 0; padding: 10px; background: #F8FDFF; border-radius: 5px; border-left: 4px solid #1E90FF; } label { display: block; font-weight: bold; margin-bottom: 8px; color: #2D5986; } input[type="text"] { width: 96%; padding: 9px 12px; border: 1px solid #87CEEB; border-radius: 4px; font-size: 15px; transition: border 0.3s; } input[type="text"]:focus { outline: none; border-color: #1E90FF; box-shadow: 0 0 8px rgba(30, 144, 255, 0.3); } #outputText { background-color: #F0FDFF; font-weight: bold; color: #0066CC; } .btn-group { text-align: center; margin-top: 20px; } button { background: #1E90FF; color: white; border: none; padding: 10px 25px; cursor: pointer; border-radius: 4px; font-size: 14px; font-weight: bold; transition: all 0.3s; margin: 0 10px; } button:hover { background: #0066CC; transform: translateY(-2px); box-shadow: 0 3px 8px rgba(0,0,0,0.15); } .footer { text-align: center; margin-top: 25px; color: #777; font-size: 12px; } .note { background: #FFF8E1; padding: 10px; border-radius: 4px; margin-top: 15px; border-left: 3px solid #FFC107; } </style> </head> <body> <div class="container"> <h1>ID转DCM即时转换器</h1> <div class="input-group"> <label for="inputText">输入文本:</label> <input type="text" id="inputText" placeholder="输入包含ID=的字符串(按回车转换)"> </div> <div class="input-group"> <label for="outputText">转换结果:</label> <input type="text" id="outputText" readonly> </div> <div class="btn-group"> <button onclick="convertID()">转换</button> <button onclick="clearFields()">清空</button> </div> <div class="note"> <strong>使用说明:</strong> 在输入框中输入包含"ID=数字"的文本,按回车键或点击转换按钮,会将ID转换为dcm格式 </div> </div> <div class="footer"> HTA ID转换插件 © 2023 </div> <script language="VBScript"> ' === 主转换函数 === Sub convertID() Dim inputStr, outputStr inputStr = Trim(document.getElementById("inputText").value) If inputStr = "" Then MsgBox "请输入包含ID=的字符串!", vbExclamation, "输入为空" Exit Sub End If ' 使用正则表达式匹配ID=后面的数字 Dim regEx, matches Set regEx = New RegExp regEx.Pattern = "ID=(\d+)" ' 匹配ID=后接1个或多个数字 regEx.IgnoreCase = True ' 忽略大小写 regEx.Global = True ' 全局匹配 ' 执行匹配 Set matches = regEx.Execute(inputStr) If matches.Count > 0 Then outputStr = inputStr ' 替换所有匹配项 For Each match In matches outputStr = Replace(outputStr, match.Value, "dcm=" & match.SubMatches(0)) Next document.getElementById("outputText").value = outputStr Else MsgBox "未找到ID=数字格式!", vbExclamation, "错误提示" document.getElementById("outputText").value = "" End If End Sub ' === 清空按钮函数 === Sub clearFields() document.getElementById("inputText").value = "" document.getElementById("outputText").value = "" document.getElementById("inputText").focus() End Sub ' === 初始化设置(页面加载时自动聚焦输入框) === Sub Window_OnLoad document.getElementById("inputText").focus() End Sub </script> <script language="JavaScript"> // 处理键盘回车键事件 (JavaScript) document.getElementById('inputText').onkeypress = function(e) { // 检测按下的键是否为回车(Enter)键 if (e.keyCode === 13 || e.which === 13) { // 调用VBScript函数 convertID() return false; // 阻止默认表单提交行为 } }; </script> </body> </html> ``` ### 核心功能说明 1. **回车键触发转换** ```javascript // JavaScript键盘事件处理 document.getElementById('inputText').onkeypress = function(e) { if (e.keyCode === 13) { // 13为回车键的键码 convertID(); // 调用转换函数 return false; // 阻止表单提交 } }; ``` 2. **正则表达式匹配转换** ```vbscript ' VBScript正则表达式处理 Set regEx = New RegExp regEx.Pattern = "ID=(\d+)" ' 匹配模式:ID=后接数字 regEx.Global = True ' 全局匹配(替换所有出现) For Each match In matches outputStr = Replace(outputStr, match.Value, "dcm=" & match.SubMatches(0)) Next ``` 3. **输入/输出框交互** - 输入框:接收用户输入(支持回车触发) - 输出框:显示转换结果(只读模式) - 清空按钮:一键重置界面 ### 使用说明 1. **保存文件**: - 复制代码到记事本 - 保存为 `ID转换器.hta` - 编码选择 `UTF-8` 2. **运行方法**: - 双击HTA文件运行 - 在输入框输入含`ID=`的字符串(如 `product?ID=12345&type=1`) - 按 `Enter` 键或点击"转换"按钮 - 查看转换结果(如 `product?dcm=12345&type=1`) 3. **正则表达式说明**: - 表达式:`ID=(\d+)` - 匹配示例:`ID=2023` → 捕获组`2023` - 转换结果:`dcm=2023` ### 功能演示 | 输入示例 | 输出结果 | |----------|----------| | `item?ID=789` | `item?dcm=789` | | `用户ID=4567` | `用户dcm=4567` | | `订单ID=2023-001` | `订单dcm=2023-001` | | `ID=98765` | `dcm=98765` |
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值