JavaScript验证 - onClick

时间:2014-04-28 19:52:31

标签: javascript html validation

我有以下代码,在单击删除图像时删除文档

<tr> To replace the document, you will need to first delete the current one. 
      <form name="delete_attachment_form" action="apr_attachment.cfc?method=delete_apr_attachment" method="post">
        <input type="hidden" name="apr_attachment_id" value="#apr_attachment_id#">
        <input type="hidden" name="apr_section_id" value="#apr_section_id#">
        <input type="hidden" name="submit_mode" value="DELETE">
         <input type="image" name="submit" src="images/delete.gif" alt="DELETE"> 
      </form> 
</tr>   

现在我想修改它并将图像更改为按钮并进行一些基本的JS验证,以便在删除之前询问确认消息。

到目前为止,我已将此作为我的代码

<tr> To replace the document, you will need to first delete the current one. 
      <form name="delete_attachment_form" action="apr_attachment.cfc?method=delete_apr_attachment" method="post">
        <input type="hidden" name="apr_attachment_id" value="#apr_attachment_id#">
        <input type="hidden" name="apr_section_id" value="#apr_section_id#">
        <input type="hidden" name="submit_mode" value="DELETE">
        <input type="button" onClick ="return confirm('Are you sure you want to delete the current project activities document')"  name="submit" Value="Delete"> 
      </form> 
</tr>   

但是当我点击此按钮时,页面中没有任何变化/效果,任何人都可以指出这里发生了什么?感谢

2 个答案:

答案 0 :(得分:0)

id="form"添加到<form>,然后

<input type="button" onClick ="if(confirm('Are you sure you want to delete the current project activities document')==1){document.getElementById('form').submit();}"  name="submit" Value="Delete">

答案 1 :(得分:0)

其他JavaScript进入的地方......或者是对PHP文件的AJAX调用,以便为您执行该任务。我可以给你一些指示吗?首先,提供button元素和ID,例如id="deleter"。然后,要清理内联标记,请在<head>部分添加此eventListener:

<script>
    var d = document.getElementById('deleter');
    var f = document.getElementByName('delete_attachment_form');
    d.onClick = function() {
        var yn = confirm('Are you sure you want to delete the current project activities document?');
         // Note that the 'confirm' dialog is an obsolete construct
        if (yn) {
            f.submit();
        }
    };
</script>

HTML标记

<tr>To replace the document, you will need to first delete the current one. 
      <form name="delete_attachment_form" action="apr_attachment.cfc?method=delete_apr_attachment" method="post">
        <input type="hidden" name="apr_attachment_id" value="#apr_attachment_id#">
        <input type="hidden" name="apr_section_id" value="#apr_section_id#">
        <input type="hidden" name="submit_mode" value="DELETE">
        <input type="button" id="deleter" name="deleter" Value="Delete"> 
      </form> 
</tr>