Javascript 如何防止页面在取消选项时重新加载或刷新?

Javascript 如何防止页面在取消选项时重新加载或刷新?,javascript,forms,submit,confirm,onsubmit,Javascript,Forms,Submit,Confirm,Onsubmit,我试图阻止用户单击“取消”选项时重新加载或提交页面。我环顾了stackoverflow,对我遇到的问题使用了相同的语法,但我尝试了类似的方法,它仍然提交并重新加载cancel选项上的页面。如何防止它执行任何操作,并保持页面处于“取消”状态 JavaScript代码: // create an input element var frm = document.getElementById("result"); var submitBtn = document.createElement("inpu

我试图阻止用户单击“取消”选项时重新加载或提交页面。我环顾了stackoverflow,对我遇到的问题使用了相同的语法,但我尝试了类似的方法,它仍然提交并重新加载cancel选项上的页面。如何防止它执行任何操作,并保持页面处于“取消”状态

JavaScript代码:

// create an input element
var frm = document.getElementById("result");
var submitBtn = document.createElement("input");
submitBtn.type = "submit";
submitBtn.value = "Confirm Purchase";
frm.appendChild(submitBtn);
submitBtn.addEventListener('click', function()
{
    // confirm the data
    var submitQ = confirm('Are you sure you want to submit your DSLR selections?');
    if(submitQ)
    {
    alert('Your query has been submitted! Have a great day! :)');
    }
    else document.getElementById("result").onsubmit = false; // prevent the page from refreshing on Cancel option
});
<form id="result" onsubmit="true">
    <!--Store User Selection Text Node Element Here-->
        <!--<p>Your DSLR budget range selected was:</p>
    <p>The DSLR brand you selected was:</p>
    <p>The type of photography you selected was:</p>
    <p>The type of lenses you selected was:</p>
    <input type="submit" value="Confirm Purchase"/>
    <input type="button" value="Reset All"/>-->
</form>
HTML代码:

// create an input element
var frm = document.getElementById("result");
var submitBtn = document.createElement("input");
submitBtn.type = "submit";
submitBtn.value = "Confirm Purchase";
frm.appendChild(submitBtn);
submitBtn.addEventListener('click', function()
{
    // confirm the data
    var submitQ = confirm('Are you sure you want to submit your DSLR selections?');
    if(submitQ)
    {
    alert('Your query has been submitted! Have a great day! :)');
    }
    else document.getElementById("result").onsubmit = false; // prevent the page from refreshing on Cancel option
});
<form id="result" onsubmit="true">
    <!--Store User Selection Text Node Element Here-->
        <!--<p>Your DSLR budget range selected was:</p>
    <p>The DSLR brand you selected was:</p>
    <p>The type of photography you selected was:</p>
    <p>The type of lenses you selected was:</p>
    <input type="submit" value="Confirm Purchase"/>
    <input type="button" value="Reset All"/>-->
</form>


也许您应该尝试将事件附加到表单提交事件,而不是单击按钮,然后只返回false返回true更改事件处理程序以处理表单提交事件,而不是提交按钮
单击事件。如果用户取消,则返回false:

frm.onsubmit = function(){
    // confirm the data
    var submitQ = confirm('Are you sure you want to submit your DSLR selections?');
    if(submitQ)
    {
    alert('Your query has been submitted! Have a great day! :)');
    } else {
       // prevent the page from refreshing on Cancel option
       return false;
    }
};

看小提琴->

你确定吗?您似乎没有在Looong fiddle中使用onsubmit处理程序:)这里有一个更新,您可以自己查看页面是否重新加载->谢谢,终于找到了!似乎
submitBtn.addEventListener
无法识别该事件,因为它只是一个按钮,仅此而已。与使用
frm.onsubmit
相反,在表单的onsubmit事件中使用该值。谢谢,我会记住的!