2

我有一个功能可以检查是否通过按下按钮填充了值,如果没有全部完成我想取消按钮的href。

我试过这个:

<a href="XXX.html"  data-role="button" data-transition="none"
                data-theme="b" onclick="Check(); return false;" data-icon="forward" data-iconpos="left">
            </a>

并且还试图从函数中返回 false。

两种方式都行不通!

我可以取消href的另一种方法是什么?

我尝试了这些事情:

function Check(event) {

    if (Testing the values ​​filled) {
     //Do something
    }
    else {
//One of the two following lines
        event.preventDefault();
        $("#xxx .ui-btn").unbind('click');
    }

}

即使那些东西不起作用,他也会来其他但它并没有取消href

4

4 回答 4

2

您需要使用event.preventDefault()

现场演示

使用jquery绑定事件

$('a[data-role=button]').click(Check);

Javascript函数

function Check(event)
{
   //Your code
   if(condition) // some condition if required.
     event.preventDefault()
}
于 2012-12-05T06:38:36.377 回答
1

你可以使用unbind

function Check(){
   if(condition){
      $('a[data-role=button]').unbind('click');
   }
}
于 2012-12-05T06:52:54.513 回答
1

您也可以使用 die() 事件代替 unbind 事件。

function Check(){
  if(condition){
      $('a[data-role=button]').die('click');
  }
}
于 2012-12-05T07:05:51.820 回答
0

我解决了如下问题:

function Check() {
  if (Testing the values ​​filled) {
   //Do something
  }
  else {
   $("#xxx .ui-btn").attr('href', "javascript:void(0)");
  }

谢谢大家的帮助

于 2012-12-05T07:08:46.657 回答