jQueryMobile将click事件添加到按钮上,而不是更改页面。

|
<p><a href=\"index.html\" data-role=\"button\" data-icon=\"arrow-r\" data-iconpos=\"right\" data-theme=\"a\" onclick=\"doSomething(); return false\">VERIFY</a></p>
我正在使用上面的代码,它是一个带有onClick()设置的jQM按钮。调用onclick并执行doSomething(),但之后,jQM显示“错误加载页面”消息。 我如何抑制该错误?在这种情况下,我需要jQM按钮,但不希望它更改页面。 谢谢     
已邀请:
由于您使用的是jQuery,因此建议您也使用jQuery来关联事件。话虽这么说,使用
e.preventDefault();
e.stopImmediatePropagation();
应该会阻止jQuery mobile对ѭ3performing执行默认操作。
$(\"#verify\").click(function (e) {
    e.stopImmediatePropagation();
    e.preventDefault();
    //Do important stuff....
});
更新资料 使用现有标记的更好方法是在
<a/>
中简单添加
rel=\"external\"
,而
onclick
的行为应正确。
<p>
  <a href=\"index.html\" data-role=\"button\" data-icon=\"arrow-r\" data-iconpos=\"right\" data-theme=\"a\" onclick=\"doSomething(); return false\" rel=\"external\">VERIFY</a>
</p>
这将起作用,因为jQuery Mobile将链接视为普通的
<a/>
标记,并返回false只会停止默认操作。     
我认为您的问题是您对按钮执行了多项操作,并且正在使用定位标记。单击该按钮时,您正在调用页面以过渡到index.html和onClick事件。
<a 
    href=\"index.html\"                       <-- go to index.html
    data-role=\"button\" 
    data-icon=\"arrow-r\" 
    data-iconpos=\"right\" 
    data-theme=\"a\" 
    onclick=\"doSomething(); return false\">  <-- Click event
VERIFY
</a>
可以尝试(可能需要删除/添加其他一些属性)
<input
    type=\"button\"
    name=\"verify\"
    id=\"verify\"
    data-icon=\"arrow-r\" 
    data-iconpos=\"right\" 
    data-theme=\"a\" 
    value=\"VERIFY\" />
现在添加一个点击事件
$(\'#verify\').click(function() {
    alert(\'Button has been clicked\');
});
实时示例:http://jsfiddle.net/vRr82/2/     
我认为您应该在锚标签中附加
data-ajax=\"false\"
。 因为在jQuery mobile中,页面转换是由AJAX完成的,对于页面转换 它一定是错误的。     
使用Jquery live函数。这对我来说非常有用     

要回复问题请先登录注册