【问题标题】:it is still submitting page even though I clicked on Cancel in confirm box即使我在确认框中单击了取消,它仍在提交页面
【发布时间】:2012-09-18 01:28:25
【问题描述】:

您好,下面我有一个提交按钮和一个在单击提交按钮时执行的 jquery 函数:

<p><input id="submitBtn" name="submitDetails" type="submit" value="Submit Details" onClick="return myClickHandler();" /></p>

<script type="text/javascript"> 
function myClickHandler()
{
     if (validation())
     {
         showConfirm();
         return true;
     }

     return false;
}
</script>

现在你可以看到validation()函数是否满足,那么它将执行showConfirm()函数,该函数将执行下面的确认框:

function showConfirm()
{
    var confirmMsg=confirm("Make sure that your details are correct, once you proceed after this stage you would not be able to go back and change any details towards Questions, Options and Answers for your Assessment." + "\n" + "\n" + "Are you sure you want to Proceed?" + "\n" );

    if (confirmMsg == true)
    {
        submitform(); 
        return true;  
    }
    else
    {
        return false;
    }
}

function submitform()
{
    var fieldvalue = $("#QandA").val();
    $.post("insertQuestion.php", $("#QandA").serialize(), function(data)
    {
        var QandAO = document.getElementById("QandA");
        QandAO.submit();
    });  
    alert("Your Details for this Assessment has been submitted"); 
}

我的问题是这样的。如果用户在确认框中单击“确定”,则它会提交该页面,这很好。但是如果用户点击取消,那么它不应该提交页面。问题是即使单击了“取消”按钮,它仍在提交页面。为什么是这样?

更新:

我已经尝试了下面的代码,但仍然没有运气:

function myClickHandler()
{
     if(validation())
     {
         return showConfirm();
     }

     return false;
}

【问题讨论】:

    标签: jquery


    【解决方案1】:
    if(validation()){
        showConfirm();
        return true;
    }
    

    您不会对来自showConfirm(); 的返回做任何事情。完成该功能后,您只需return true。试试return showConfirm();


    如果用户在showConfirm()函数中点击取消,则不执行submitform()函数,showConfirm()函数返回false。但是由于您没有捕获该返回值,myClickHandler() 函数返回 true,这不会阻止表单被提交。

    您可能只是希望myClickHandler() 函数始终返回false,并让您的showConfirm() 函数处理表单提交。


    更新: 这是(IMO)更好的方法。我不确定您是否需要使用 AJAX($.post 调用)。尝试这个:
    <form id="QandA" action="somepage.php" method="POST" onsubmit="return myClickHandler()">
        <!--
            ...
        -->
        <input type="submit" id="submitBtn" name="submitDetails" value="Submit Details" />
    </form>
    
    <script type="text/javascript">
        function myClickHandler()
        {
            if (!validation())
                return false;
    
            if (!confirm("Make sure that your details are correct, once you proceed after this stage you would not be able to go back and change any details towards Questions, Options and Answers for your Assessment." + "\n" + "\n" + "Are you sure you want to Proceed?" + "\n" ))
                return false;
    
            // if all you're trying to do is submit the form to insertQuestion.php,
            // then skip this AJAX call. I'm assuming that you need to do something
            // on the server before submitting the form. if not, skip this.
            $.ajax({
                url: "insertQuestion.php",
                data: $("#QandA").serialize(),
                async: false
            });
    
            return true;
        }
    </script>
    

    【讨论】:

    • 我在尝试return showConfirm(); 代码的问题中包含了更新,但仍然没有运气。
    • 好的,我应该这样做function myClickHandler(){ if(validation()){ showConfirm(); return false; } 然后执行 `function showConfirm(){ var confirmMsg=confirm("......" ); if (confirmMsg==true) { submitform();返回真; } } } `
    • 如果您这样做,您不会因为单击提交按钮而阻止提交表单。您需要 myClickHandler 函数返回 false。也许尝试将事件更改为onmousedownonmouseup 或将事件移动到表单元素并使用onsubmit
    • 你能告诉我你的意思,把事件移动到表单elemant并使用onsubmit,我对此有点困惑,哈哈
    • @user1653070:我用我认为更好的解决方案更新了我的答案。它看起来更简洁明了,而不是跟踪谁将什么返回到哪里。
    猜你喜欢
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多