gpt4 book ai didi

javascript - 如何让html页面检测回车键被按下?

转载 作者:行者123 更新时间:2023-11-30 23:44:49 25 4
gpt4 key购买 nike

我正在使用 VSTS 2008 + C# + .Net 3.5 + IIS 7.0 + ASP.Net 来开发 Web 应用程序。该 Web 应用程序有一个名为 default.aspx 的 aspx 页面。我想实现当用户按下键盘的回车键时,与按下“Action”按钮的效果相同的效果。有什么想法如何实现吗?

这是default.aspx文件,

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>

<script type="text/javascript">
function requery() {
var query = location.search.substring(1);
var pairs = query.split("&");
var param1Value = document.getElementById("txtParam1").value;

url = "/Default.aspx?param1=" + param1Value;
for (var i = 0; i < pairs.length; ++i) {
var pair = pairs[i];
if ((pair.indexOf("param1") != 0) && (pair.length > 0)) {
url += "&" + pair;
}
}
location.href = url;
}
</script>

<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtParam1" runat="server"></asp:TextBox>
<input type="button" value="Action" onclick="requery()" />
</div>
</form>
</body>
</html>

这是文件default.aspx.cs背后的代码,

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtParam1.Text = Request.QueryString["param1"];
}
}
}

最佳答案

向文档的 keydown 事件添加一个事件处理程序。评论里有解释。

    function keydownHandler(e) {

if (e.keyCode == 13) { // 13 is the enter key

requery(); // call your function.

// alternately, give your button an id and used
// document.getElementById('myID').click();

// prevent the browser from doing whatever it normally
// does when you push the enter key.
// (not tested for this situation, may need tweaking)
if (e.preventDefault) {
e.preventDefault(); // FF,chrome
}
else {
return false; // IE
}
}
}

// register your handler method for the keydown event
if (document.addEventListener) {
document.addEventListener('keydown', keydownHandler, false);
}
else if (document.attachEvent) {
document.attachEvent('onkeydown', keydownHandler);
}

关于javascript - 如何让html页面检测回车键被按下?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3372488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com