2012-11-27 43 views
0

我在使用asp.Net的网站上工作,它包含一个从iframe调用的页面。 这个页面命名为Districting,在aspx页面有一个javascript代码。 我创建了一个函数,当点击“完成”按钮时执行该函数。此功能测试用户所做的条件是否为真。 如果是,则加载另一个页面,如果不是,则会出现警报,并且不发生任何事情。 但实际上,发生了一些事情:页面正在刷新,这不是我想要的。 这是按钮:如何不从JavaScript刷新页面?

<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 
    } 
} 

因此,所有我需要的是阻止该网页上的“完成”按钮,点击时刷新。

感谢提前:)

回答

1

更改您的代码

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

和你的标签

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

而且,看到How to confirm navigation for a link in a href tag?,因为它是非常相似的。

+0

非常感谢:)你真的帮助了! – Hanady

+0

不客气,很高兴我可以帮助:) – MadSkunk

0

按钮是标准的提交按钮,所以是在形式的按钮?

如果这是真的,你可以在定义按钮的类型takkle这个问题:

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

你说的生火的onclick需要返回false javascript函数。下面应该工作:

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