如何不从JavaScript刷新页面? [英] How not to refresh a page from JavaScript?

查看:65
本文介绍了如何不从JavaScript刷新页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用asp.Net在一个网站上工作,它包含一个从iframe调用的页面. 该页面名为Districting,在aspx页面中具有javascript代码. 我创建了一个函数,该函数在单击完成"按钮时执行.此功能测试用户所设定的条件是否正确. 如果是,则加载另一个页面;如果否,则显示警报,并且什么也没有发生. 但是实际上,正在发生一些事情:页面令人耳目一新,这不是我想要的. 这是按钮:

I am working on a website using asp.Net, and it includes a page called from an iframe. This page named Districting, has a javascript code in the aspx page. I created a function, that is executed when "Done" button is clicked. This function tests if the conditions that the user made are true. If yes, another page is loaded, if no, an alert appears, and nothing happens. But actually, something is happening: the page is refreshing which is not what I want. This is the button:

<button id="Button1" onclick="testing()">Done</button>

这是功能测试:

function testing() {
    if (conditions are true)
        //Go to another page
    else{
        alert("Wrong decision");
        //Stay in the current page without refreshing it
    }
 }

因此,我要做的就是防止单击完成"按钮时刷新页面.

So all I need is to prevent the page from refreshing when clicking on "Done" button.

提前感谢:)

推荐答案

将代码更改为

function testing() {
    if (conditions are true)
        return true;
    else{
        alert("Wrong decision");
        return false;
    }
 }

和您的标签

<button id="Button1" onclick="return testing()">Done</button>    

另外,请参见 ,因为它非常相似,因此如何确认href标记中的链接的导航?

Also, see How to confirm navigation for a link in a href tag?, as it's very similar.

这篇关于如何不从JavaScript刷新页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆