简单的JavaScript问题:onClick确认不会阻止默认操作 [英] Simple JavaScript problem: onClick confirm not preventing default action

查看:111
本文介绍了简单的JavaScript问题:onClick确认不会阻止默认操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过一个带有确认对话框的onClick事件制作一个简单的删除链接。我想确认用户想要删除一个条目。但是,似乎在对话框中单击取消时,默认操作(即href链接)仍在进行,因此该条目仍会被删除。不知道我在这里做错了什么...任何输入将不胜感激。编辑:其实,现在代码的方式,页面没有即使进行函数调用......所以,根本没有对话出现。我确实有onClick代码:

  onClick =confirm('Delete entry?')
code>

确实会弹出一个对话框,但仍然会打开取消链接。

 <%@ taglib prefix =curi =http://java.sun.com/jstl/core_rt%> 
<%@ taglib prefix =fmturi =http://java.sun.com/jstl/fmt_rt%>
<%@ taglib uri =http://java.sun.com/jsp/jstl/functionsprefix =fn%>

< script type =text / javascript>

function delete(){
return confirm('Delete entry?')
}

< / script>


...

< tr>
< c:if test =$ {userIDRO}>
< td>
< a href =showSkill.htm?row =< c:out value =$ {skill.employeeSkillId}/> />
< / td>
< td>
< a href =showSkill.htm?row =< c:out value =$ {skill.employeeSkillId}& remove = 1/> onClick =return delete()/>
< / td>
< / c:if>
< / tr>


解决方案

您的代码中存在拼写错误(标记 a 过早关闭)。
您可以使用:

 < a href =whateveronclick =return confirm('you确定?')>< img ...>< / a> 

请注意 return(confirm):内部脚本返回的值evens决定默认的浏览器动作是否运行;如果您需要运行一大段代码,您当然可以调用另一个函数:

 < script type =text / JavaScript的> 
函数confirm_delete(){
return confirm('你确定吗?');
}
< / script>
...
< a href =whateveronclick =return confirm_delete()>< img ...>< / a>

(注意delete是关键字)

<为了完整性:现代浏览器还支持DOM事件,允许您为每个对象上的相同事件注册多个处理程序,访问事件的详细信息,停止传播等等。请参阅 DOM Events


I'm making a simple remove link with an onClick event that brings up a confirm dialog. I want to confirm that the user wants to delete an entry. However, it seems that when Cancel is clicked in the dialog, the default action (i.e. the href link) is still taking place, so the entry still gets deleted. Not sure what I'm doing wrong here... Any input would be much appreciated.

EDIT: Actually, the way the code is now, the page doesn't even make the function call... so, no dialog comes up at all. I did have the onClick code as:

onClick="confirm('Delete entry?')"

which did bring up a dialog, but was still going to the link on Cancel.

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt_rt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<script type="text/javascript">

function delete() {
    return confirm('Delete entry?')
}

</script>


...

<tr>
 <c:if test="${userIDRO}">
    <td>
        <a href="showSkill.htm?row=<c:out value="${skill.employeeSkillId}"/>" />
        <img src="images/edit.GIF" ALT="Edit this skill." border="1"/></a>
    </td>
    <td>
        <a href="showSkill.htm?row=<c:out value="${skill.employeeSkillId}&remove=1"/>" onClick="return delete()"/>
        <img src="images/remove.GIF" ALT="Remove this skill." border="1"/></a>
    </td>
 </c:if>
</tr>

解决方案

There's a typo in your code (the tag a is closed too early). You can either use:

<a href="whatever" onclick="return confirm('are you sure?')"><img ...></a>

note the return (confirm): the value returned by scripts in intrinsic evens decides whether the default browser action is run or not; in case you need to run a big piece of code you can of course call another function:

<script type="text/javascript">
function confirm_delete() {
  return confirm('are you sure?');
}
</script>
...
<a href="whatever" onclick="return confirm_delete()"><img ...></a>

(note that delete is a keyword)

For completeness: modern browsers also support DOM events, allowing you to register more than one handler for the same event on each object, access the details of the event, stop the propagation and much more; see DOM Events.

这篇关于简单的JavaScript问题:onClick确认不会阻止默认操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆