如果表单提交不是通过特定的按钮提交的,例如
- 通过按下Enter键
- 在JS中使用
HTMLFormElement.submit()
那么浏览器应该如何确定哪个(如果有)提交按钮要用作按下的按钮?这在两个方面都很重要:
- 调用附加到提交按钮的
onclick
事件处理程序 - 发送回Web服务器的数据
到目前为止,我的实验表明:
- 按下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>