【问题标题】:jquery mobile button automatically refreshes pagejquery mobile按钮自动刷新页面
【发布时间】:2012-01-16 22:22:44
【问题描述】:

我使用 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");

}

我收到警报,然后页面刷新。

【问题讨论】:

    标签: javascript html jquery-mobile postback


    【解决方案1】:

    return false; 添加到BtnRatePressed() 的末尾,这样点击甚至不会传播,也不会导致表单提交。

    编辑:另外,将您的属性更改为onclick="return BtnRatePressed();"

    【讨论】:

      【解决方案2】:

      问题是您使用的是实际按钮并且导致表单动作,您可以通过使用这样的超链接按钮来完成相同的事情并避免问题...

      <a href="javascript:BtnRatePressed();" data-role="button" id="rateButton" data-theme="a" data-icon="star" data-iconpos="right">Rate</a>
      

      【讨论】:

        【解决方案3】:

        我们需要代码来找出答案。有什么方法可以为我们提供现场演示?

        您可能忘记在 onclick 事件结束时返回 false。忽略 false 的返回将使浏览器无论如何都会执行链接的 href,这可能会显示为页面重新加载。

        正如我所说,查看实时示例,将 return false 添加到任一

        你的 BtnRatePressed 函数

        function BtnRatePressed()
        {
            var rateBtn = document.getElementById('rateButton');
            rateBtn.style.visibility = 'hidden';
            alert("yeh");
            return false;
        }
        

        或在 onclick 语句中。

        <button id="rateButton" data-theme="a" data-icon="star" data-iconpos="right" onclick="BtnRatePressed(); return false;">Rate</button>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-20
          • 2018-11-12
          • 2012-05-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多