【发布时间】:2010-10-29 20:20:11
【问题描述】:
如果提交了表单但不是通过任何特定按钮,例如
- 按Enter
- 在 JS 中使用
HTMLFormElement.submit()
浏览器应该如何确定多个提交按钮中的哪一个(如果有)用作按下的按钮?
这在两个层面上很重要:
- 调用附加到提交按钮的
onclick事件处理程序 - 发回网络服务器的数据
到目前为止,我的实验表明:
- 当按下 Enter 时,Firefox、Opera 和 Safari 使用表单中的第一个提交按钮
- 当按下 Enter 时,IE 使用第一个提交按钮或根本不使用,具体取决于我无法弄清楚的条件
- 所有这些浏览器都不使用任何 JS 提交
标准是怎么说的?
如果有帮助,这是我的测试代码(PHP 仅与我的测试方法相关,与我的问题本身无关)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
</head>
<body>
<h1>Get</h1>
<dl>
<?php foreach ($_GET as $k => $v) echo "<dt>$k</dt><dd>$v</dd>"; ?>
</dl>
<h1>Post</h1>
<dl>
<?php foreach ($_POST as $k => $v) echo "<dt>$k</dt><dd>$v</dd>"; ?>
</dl>
<form name="theForm" method="<?php echo isset($_GET['method']) ? $_GET['method'] : 'get'; ?>" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<input type="text" name="method" />
<input type="submit" name="action" value="Button 1" onclick="alert('Button 1'); return true" />
<input type="text" name="stuff" />
<input type="submit" name="action" value="Button 2" onclick="alert('Button 2'); return true" />
<input type="button" value="submit" onclick="document.theForm.submit();" />
</form>
</body></html>
【问题讨论】:
标签: javascript html cross-browser standards