如何禁用点击事件

时间:2012-11-25 23:02:03

标签: javascript jquery html

function insertQuestion(form) {
    var x = "<img src='Images/plussigndisabled.jpg' width='30' height='30' alt='Look Up Previous Question' class='plusimage' name='plusbuttonrow'/><span id='plussignmsg'>(Click Plus Sign to look <br/> up Previous Questions)</span>";
    if (qnum == <? php echo(int) $_SESSION['textQuestion']; ?> ) {
        $('#mainPlusbutton').replaceWith(x);
    }
    //append rows into a table code, not needed for this question
}....
$('.plusimage').live('click', function () {
    plusbutton($(this));
});

function plusbutton(plus_id) {
    // Set global info
    plusbutton_clicked = plus_id;
    // Display an external page using an iframe
    var src = "previousquestions.php";
    $.modal('<iframe src="' + src + '" style="border:0;width:100%;height:100%;">');
    return false;
}
<form id="QandA" action="<?php echo htmlentities($action); ?>" method="post">
<table id="question">
  <tr>
    <td colspan="2">
      <a onclick="return plusbutton();">
        <img src="Images/plussign.jpg" width="30" height="30" alt="Look Up Previous Question" class="plusimage" id="mainPlusbutton" name="plusbuttonrow"/>
      </a>
      <span id="plussignmsg">
        (Click Plus Sign to look up Previous Questions)
      </span>        
    </td>
  </tr>
</table>
<table id="questionBtn" align="center">
  <tr>
    <th>
      <input id="addQuestionBtn" name="addQuestion" type="button" value="Add Question" onClick="insertQuestion(this.form)" />
    </th>
  </tr>
</table>
</form>

在上面的代码中,当满足if语句时,我可以用另一个图像替换图像。但我的问题是,当图像被替换时,它不会禁用点击事件。我的问题是,当替换图像时,如何禁用onclick事件onclick="return plusbutton();

在这种情况下可以取消绑定点击工作。我不想使用href=#,因为我不想在网址末尾包含#

3 个答案:

答案 0 :(得分:1)

您应该使用$('.plusimage').off('click');来禁用点击事件(在jQuery&lt; 1.7上弃用unbind和live,但仍然支持)。

如果您正在寻找一种只允许点击一个元素的方法,您可以使用它:

function insertQuestion(form) {   

    var x = "<img src='Images/plussigndisabled.jpg' width='30' height='30' alt='Look Up Previous Question' class='plusimage' name='plusbuttonrow'/><span id='plussignmsg'>(Click Plus Sign to look <br/> up Previous Questions)</span>" ;

    if (qnum == <?php echo (int)$_SESSION['textQuestion']; ?>) {
       $('#mainPlusbutton').replaceWith(x);
       //If the condition hold, then deactivate the click event!
       $('form table a').addClass('disabled');
    }
    else{
       //If the condition is false, click event is activated removing the disabled class
       $('.form table a').removeClass('disabled');
    }
    //append rows into a table code, not needed for this question
}

添加此Click事件并删除onclick="return plusbutton();内部链接元素:

//Click event activated only if the disabled class is not set
$('form table a:not(.disabled)').on('click', function() {
   plusbutton($(this));
});

答案 1 :(得分:0)

$('.plusimage').live('click', function() {
  if (qnum != <?php echo (int)$_SESSION['textQuestion']; ?>) {
    plusbutton($(this));
  }
});

答案 2 :(得分:0)

也许这不完全正确,因为它没有在JsFiddle中运行,但如果你加入Tom的问题,应该给你一些提示。

http://jsfiddle.net/jeanpaul1982/38hjZ/15/

也许原因是我删除了一些php,因为没有办法回应或检查会话变量,虽然我相信你应该首先检查$ _SESSION变量是否存在。

@add Tom:对PHP echo进行了一些小修改

function insertQuestion(form) {   

    var x = "<img src='Images/plussigndisabled.jpg' width='30' height='30' alt='Look Up Previous Question' class='plusimage' name='plusbuttonrow'/><span id='plussignmsg'>(Click Plus Sign to look <br/> up Previous Questions)</span>" ;

 <?
 //to avoid typecasting just declare the variable as a number
 $text_question_res=0;

session_start(); //If session has not started previously
if(isset($_SESSION['textQuestion']) ) $text_question_res.=$_SESSION['textQuestion'];
echo <<<EOD

if (qnum == $text_question_res) //expected to be a number

EOD;

//    if (qnum == <?php echo (int)$_SESSION['textQuestion']; ?>) {


       $('#mainPlusbutton').replaceWith(x);
       //If the condition hold, then deactivate the click event!
       $('.plusimage').addClass('disabled');
    }
    else{
       //If the condition is false, click event is activated removing the disabled class
       $('.plusimage').removeClass('disabled');
    }
    //append rows into a table code, not needed for this question
}