HTML/Javascript onchange 与 onsubmit 问题

发布于 2024-11-15 19:30:02 字数 1527 浏览 6 评论 0原文

我正在尝试一个代码,它将加载一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

﹉夏雨初晴づ 2024-11-22 19:30:02

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

Onsubmit goes on the form, not on the submit button, try using type="button" instead in an "onclick" event.

︶葆Ⅱㄣ 2024-11-22 19:30:02

OnSubmit 是一个 form 事件,因此它不适用于 input

请改用 type='button'OnClick 事件。

OnSubmit is a form event so it won't work on input.

Use type='button' and the OnClick event instead.

为你拒绝所有暧昧 2024-11-22 19:30:02

两件事:

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" />

2 things:

onsubmit belongs to the form element, not the input, so you would put your current input in a form like this:

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

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:

<input value="Song1" onclick="foo.location='song.php?song=blind_willie.mp3'"  style="display:block; margin:auto" type="Submit" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文