[英]Need help in HTML submit button and javascript(confirm-button) on django:
Scenario: I'm trying to use image button in my HTML form with JavaScript: 场景:我正在尝试将HTML表单中的图片按钮与JavaScript结合使用:
<form method="post" name="delete">
<input type="image" name="{{a.id}}"
src="http://localhost:8000/site_media/web/img/delete_icon.gif"
onClick="decision(this,'Delete this Activity?','http://localhost:8000/delAct/listAct')"/>
</form>
this is my script: 这是我的脚本:
<SCRIPT LANGUAGE="Javascript">
<!---
function decision(selectobject, message, url){
if(confirm(message)) location.href = url+"/"+selectobject.name ;
}
// --->
</SCRIPT>
The problem is it's not redirecting to page delAct/listAct
like it is supposed to do. 问题是它没有像预期的那样重定向到页面
delAct/listAct
。 So I tried to change it to use type="button"
and it's working fine: 因此,我尝试将其更改为使用
type="button"
,并且工作正常:
<form method="post" name="delete">
<input type="button" name="{{a.id}}"
onClick="decision(this,'Delete this Activity?','http://localhost:8000/delAct/listAct')"/>
</form>
My question is how to get the first script to work using type="image"
? 我的问题是如何使用
type="image"
使第一个脚本起作用? Is there any way to do that? 有什么办法吗?
As an aside: I've try both get
and post
methods in the form already and it makes no difference. 顺便说一句:我已经尝试过表单中的
get
和post
方法,并且没有区别。 Also, I'm using Python 2.6 and Django in my project. 另外,我在项目中使用Python 2.6和Django。
The problem you are having is that using an input
of type image creates a submit button that looks like an image so when it is clicked the form is submitted to the same page you are on because the <form>
tag doesn't specify an action
attribute. 您遇到的问题是,使用类型为image的
input
会创建一个外观类似于图像的提交按钮,因此当单击该表单时,该表单将提交到您所在的同一页面,因为<form>
标记未指定action
属性。 To prevent the submit from trigering you need to return false
from the decision event handler which will prevent the event from bubbling up. 为了防止提交触发,您需要从决策事件处理程序返回
false
,这将防止事件冒泡。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.