jquery移动按钮自动刷新页面
我使用 jquery mobile 创建一个移动网页。我定义了一个按钮和一个 onclick 事件。执行 onclick 函数后,我的页面会自动刷新。有谁知道为什么会发生这种情况或者我该如何阻止它?
我的按钮声明:
<button id="rateButton" data-theme="a" data-icon="star" data-iconpos="right" onclick="BtnRatePressed();">Rate</button>
功能:
function BtnRatePressed() {
var rateBtn = document.getElementById('rateButton');
rateBtn.style.visibility = 'hidden';
alert("yeh");
}
我收到警报,然后页面刷新。
I a using jquery mobile to create a mobile web page. I defined a button and an onclick event. After the onclick function is carried out my page is automatically refreshed. Does anyone know why this is happening or how i can stop it?
my button declaration:
<button id="rateButton" data-theme="a" data-icon="star" data-iconpos="right" onclick="BtnRatePressed();">Rate</button>
function:
function BtnRatePressed() {
var rateBtn = document.getElementById('rateButton');
rateBtn.style.visibility = 'hidden';
alert("yeh");
}
I receive the alert and then the page refreshes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们需要代码来找出答案。您可以通过什么方式向我们提供现场演示吗?您可能忘记在 onclick 事件结束时返回 false。忽略 false 的返回将使浏览器无论如何执行链接的 href,这可能会显示为页面重新加载。正如我所说,查看实时示例,将 return false 添加到 或
您的 BtnRatePressed 函数
onclick 语句中。
We need code for this to find out. Any way you can supply us with a live demo?You might have forgotten to return false at the end of the onclick event. Neglecting the return of false will make the browser to execute the href of the link anyway, which might appear as a page reload.As I stated, seeing the live example, add return false to either
your BtnRatePressed function
or within the onclick statement.
将
return false;
添加到BtnRatePressed()
的末尾,这样即使点击也不会传播,也不会导致表单提交。编辑:此外,将属性更改为
onclick="return BtnRatePressed();"
Add
return false;
to the end ofBtnRatePressed()
so that the click even isn't propagated and doesn't cause the form to submit.Edit: Also, change your attribute to
onclick="return BtnRatePressed();"
问题是您正在使用实际的按钮,这会导致表单动作,您可以通过使用像这样的超链接按钮来完成同样的事情并避免问题......
The problem is that you're using an actual button and that is causing the form to action, you can accomplish the same thing by using a hyperlink button like this and avoid the problem...