提交功能不适用于其他JavaScript警报框插件

时间:2017-09-03 17:34:38

标签: javascript jquery html

我的脚本工作正常,但我想让我的确认框设计得更好,但我无法使其工作,因为当我使用" document.getElementbyId(' btnApprove&#39 )提交();"在插件的回调方法中,它返回一个错误" submit()未定义"。我该怎么用 "的document.getElementById(' btnApprove&#39)。提交();"到sweetalert插件? 使用Javascript:

<script>        
function approval_confirmation(){
    If(confirm("Are you sure")==1){
       document.getElementbyId('btnApprove').submit();
    }
}
<script>

标记:

<form method="POST">
<button name="btnApprove" id="btnApprove" onClick="approval_confirmation();return false"> APPROVE</button>
</form>

(将approval_question函数转移到sweetalert2) Sweetalert 2:

  swal({ 
title: 'Are you sure?', 
text: "You won't be able to revert this!",
 type: 'warning', 
showCancelButton: true, 
confirmButtonColor: '#3085d6', cancelButtonColor: '#d33', 
confirmButtonText: 'Yes, delete it!' }).then(function () { 
swal( 'Deleted!', 'Your file has been deleted.', 'success' ) })

2 个答案:

答案 0 :(得分:0)

您必须在表单上调用submit方法,而不是按钮,即在您的情况下它应该是这样的:

<form id="approval-form" method="POST">
<button name="btnApprove" id="btnApprove" onClick="approval_confirmation();return false"> APPROVE</button>
</form>

<script>        
function approval_confirmation(){
    If(confirm("Are you sure") == 1){
       document.querySelector('#approval-form').submit();
    }
}
<script>

答案 1 :(得分:0)

您应该使用表单ID来搜索表单attr并提交

 document.getElementbyId('formId').submit();

或添加

 <form action="/action_page.php" method="get" id="form1">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
</form>

<button type="submit" form="form1" value="Submit">Submit</button>