简体   繁体   English

表单上的Java确认提交未取消

[英]Javascript Confirm on Form Submit Not Canceling

I have a pretty simple Javascript function I want to include in a form (submit or cancel). 我有一个非常简单的Javascript函数,要包含在表单中(提交或取消)。 The problem is it's supposed to cancel the form submit if I push cancel. 问题是,如果我按“取消”,则应该取消表单提交。 Right now it just gives me the appropriate warning, but continues with submitting the form whether I push "ok" or "cancel". 现在,它只是给我适当的警告,但无论我按下“确定”还是“取消”,都继续提交表格。

I'm new to JS so sorry if I'm missing something basic, but I can't seem to find a solid answer to this seemingly simple problem. 我是JS的新手,所以很抱歉,如果我缺少一些基本知识,但是对于这个看似简单的问题,我似乎找不到一个可靠的答案。

JAVASCRIPT JAVASCRIPT

function confirmdelete()
{
confirm("Deleting this answer may cause dataloss, are you sure you want to continue?")
}

PHP / HTML PHP / HTML

echo '
    <form onsubmit="confirmdelete()" class="answerform" id="deletelink" action="/admin/questions" method="post"
    onsubmit="return confirm("Do you want to delete this answer?");>
        <input type="hidden" name="ansdel" value="' . $row['id'] . '">
        <input type="hidden" name="notice" value="You have deleted: ' . $row['answer'] . '">
        <input class="answerbutton" id="deletebutton" type="submit" value="Delete">
    </form>
';

Change it to 更改为

function confirmdelete()
{
    return confirm("Deleting this answer may cause dataloss, are you sure you want to continue?")
}

Fixed the issue by putting 通过放置来解决此问题

onsubmit="return confirmdelete()"

Instead of 代替

onsubmit="confirmdelete()"

That was the main issue, also used some of Rob's updates above for the final Javascript function... Thanks! 那是主要问题,还在上面的Rob的一些更新中使用了最终的Javascript功能……谢谢!

function confirmdelete() {
    var r=confirm("Deleting this answer may cause data loss, are you sure you want to continue?");
    if (r==true)
      {
        return true;
      }
    else
      {
        return false;
      }
}

You need to combine the confirm with an if statement: 您需要将确认与if语句结合使用:

function confirmdelete() {
    if(confirm("Deleting this answer may cause dataloss, are you sure you want to continue?")({
        return true;
    } else {
        return false;
    }
}

You can also do the return that @mmillican recommended (much shorter, nice and simple), I just usually do the if statement in case you want to do some extra magic. 您还可以执行@mmillican建议的return结果(更短,更好,更简单),我通常会执行if语句,以防您想做一些额外的魔术。 Either one works. 任一个作品。 :) :)

Need to assign the confirm and return it 需要分配确认并退回

var confirmed = confirm("Deleting ...
return confirmed;

return false; is what stops the form submit 是什么阻止表单提交

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM