HTML / Javascript onchange vs onsubmit问题

时间:2011-06-16 04:45:34

标签: javascript html onchange onsubmit

我正在尝试加载iframe onsubmit的代码。我能够onchange一个文本字段(代码取自某处)。但onsubmit不起作用:

作品

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<meta name="Content-Style-Type" content="text/css">
<title>Example</title>

</head>
<body>
<input value="Song1" onchange="foo.location='song.php?song=blind_willie.mp3'"  style="display:block; margin:auto" type="text">
<iframe name="foo" style="display:block; margin:auto"></iframe>
</body>
</html>

不工作

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">


<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<meta name="Content-Style-Type" content="text/css">
<title>Example</title>

</head>
<body>
<input value="Song1" onsubmit="foo.location='song.php?song=blind_willie.mp3'"  style="display:block; margin:auto" type="Submit">
<iframe name="foo" style="display:block; margin:auto"></iframe>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

Onsubmit在表单上,​​而不是在提交按钮上,尝试在“onclick”事件中使用type =“button”。

答案 1 :(得分:1)

OnSubmitform事件,因此无法在input上使用。

改为使用type='button'OnClick事件。

答案 2 :(得分:1)

2件事:

onsubmit属于表单元素,而不是输入,因此您可以将当前输入放在这样的表单中:

<form onsubmit="foo.location='song.php?song=blind_willie.mp3'; return false">
    <input value="Song1" style="display:block; margin:auto" type="Submit" />
</form>

第二件事是你需要从onsubmit返回false,否则它会尝试刷新页面,因为你没有动作。

另一方面,这似乎是一种非常奇怪的方法。你当然可以这样做:

<input value="Song1" onclick="foo.location='song.php?song=blind_willie.mp3'"  style="display:block; margin:auto" type="Submit" />