0
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=#,因为我不想在 url 末尾包含 #

4

3 回答 3

1

您应该使用$('.plusimage').off('click');禁用单击事件(取消绑定和 live 在 jQuery < 1.7 上已弃用,但仍受支持)。

如果您正在寻找一种仅允许单击元素的方法,则可以使用以下方法:

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
}

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

//Click event activated only if the disabled class is not set
$('form table a:not(.disabled)').on('click', function() {
   plusbutton($(this));
});
于 2012-11-25T23:12:49.967 回答
0

也许它并不完全正确,因为它没有在 JsFiddle 中运行,但如果你将它与汤姆的问题一起加入,它应该会给你一些提示。

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

也许原因是我删除了一些 php,因为没有办法回显或检查 session 变量,尽管我认为你应该首先检查 $_SESSION 变量是否存在。

@add Tom:对 PHP 回显做了一些小的修改

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
}
于 2012-11-26T01:14:10.727 回答
0
$('.plusimage').live('click', function() {
  if (qnum != <?php echo (int)$_SESSION['textQuestion']; ?>) {
    plusbutton($(this));
  }
});
于 2012-11-25T23:06:57.870 回答