2011-03-14 63 views
2

与使用<a>标签的,我可以把这样的:的JavaScript的onClick HREF

<a href="include/sendemail.php?id_user=<?= $_GET['id_user'];?>" onclick="return confirm('Are you sure want to send an email?');" >Send Email</a> 

,以及如何做到这一点的代码应用到按钮:

<input type="button" onclick="return confirm('Are you sure want to send an email?');" value="Send Email" > 
+0

你有什么工作。它在做什么/不做你不指望的事情? – 2011-03-14 11:16:04

+0

*(旁注)*事件属性被认为是不好的做法。 [试着让它们不显眼。](https://secure.wikimedia.org/wikipedia/en/wiki/Unobtrusive_JavaScript) – Gordon 2011-03-14 11:21:32

+0

你是否设法解决这个问题? – 2011-03-14 11:21:38

回答

2

如果你想只实现你的目标稍微改动一下,试试这样:

<input type="button" onclick="if(confirm('Are you sure want to send an email?')) { document.location.href = 'include/sendemail.php?id_user='; } " value="Send Email" />
+0

不,你不要把'javascript:'放在'onXYZ'属性的开头。它们包含* code *,而不是URL。它的工作原理基本上是巧合(你正在定义一个*标签*)。 – 2011-03-14 11:16:36

+0

@ T.J。克劳德:谢谢,我已经更新了我的答案。 – 2011-03-14 11:18:35

0

按钮不是超链接。

要使按钮导航到不同的页面,您可以在click处理程序中编写location = 'URL';

0
<form action="include/sendemail.php" method="GET"> 
<input type="hidden" name="id_user" value="<?php echo intval($_GET['id_user']); ?>" /> 
<input type="button" onclick="return confirm('Are you sure want to send an email?');" value="Send Email" /> 
</form> 

您还可以使用<form>标记的onsubmit

2
<input type="button" onclick="return myFunc();" value="Send Email" > 

<script> 

function myFunc() 
{ 
var flag = confirm('Are you sure want to send an email?'); 
if(flag) 
    document.location.href = "include/sendemail.php?id_user=<?= $_GET['id_user'];?>" ; 
else 
    return false; 

}