HTML/Javascript onchange 与 onsubmit 问题
我正在尝试一个代码,它将加载一个 iframe onsubmit
。我能够在文本字段的onchange
上执行此操作(从某处获取的代码)。但是 onsubmit
不起作用:
Works
<!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>
DOES NOT WORK
<!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>
I am trying a code, that will load an iframe onsubmit
. I am able to do it onchange
of a text field (code taken from somewhere). But onsubmit
does not work:
Works
<!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>
DOES NOT WORK
<!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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Onsubmit 发生在表单上,而不是发生在提交按钮上,请尝试在“onclick”事件中使用 type="button" 代替。
Onsubmit goes on the form, not on the submit button, try using type="button" instead in an "onclick" event.
OnSubmit
是一个form
事件,因此它不适用于input
。请改用
type='button'
和OnClick
事件。OnSubmit
is aform
event so it won't work oninput
.Use
type='button'
and theOnClick
event instead.两件事:
onsubmit 属于表单元素,而不是输入,因此您可以将当前输入放入这样的表单中:
第二件事是您需要从 onsubmit 返回 false ,否则它将尝试刷新页面,因为您没有行动。
顺便说一句,这似乎是一种很奇怪的方法。你当然可以这样做:
2 things:
onsubmit belongs to the form element, not the input, so you would put your current input in a form like this:
The 2nd thing is that you need to return false from the onsubmit or it will try to refresh the page since you don't have an action.
On a side note, this seems like quite a strange method you have. You could certainly just do: