Onclick在jquery中不起作用

时间:2015-01-15 10:22:08

标签: jquery onclick

我想为竞赛构建一个民意调查应用程序,我不明白为什么onclick函数不起作用,我还将jquery和文件包含在页面中。

我在控制台或网络标签中没有收到任何错误。

以下是代码:

<!DOCTYPE html>
<html>
    <head>
        <title>Document</title>
        <meta charset="utf-8">
        <link rel="stylesheet" type="text/css" href="css/main.css">
    </head>
    <body>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="poll.js"></script>

    <?php if(!$poll):?>
            <p>There is not a poll or it already expired</p>
    <?php else:?>

        <div class="poll">
            <div class="poll-question">
                <?php echo htmlspecialchars($poll->question); ?>
                <div>
                    <a class="delete" href="#" onclick="return false; deletePoll(<?php echo $id; ?>);">Delete Poll</a><span class="deletion"></span> | 
                    <a href="#" onclick="return false; closePoll(<?php echo $id?>)">Close poll</a><span class="closing"></span>
                </div>
            </div>
            <div style="color: red">This poll will expire in <?php echo $poll->end;?>.</div>
            <?php if($completed): ?>
                <p>&#10003; You already voted for this poll</p>
                <ul>
                <?php foreach($answers as $answer):?>
                    <li>
                        <?php echo $answer->name; ?> (<?php echo number_format($answer->percentage, 2)?>%)
                    </li>
                <?php endforeach;?>
                </ul>
            <?php else: ?>
                <?php if(!empty($choices)):?>

                <form method="post" action="vote.php">
                    <div class="poll-options">
                        <?php foreach($choices as $index => $choice):?>
                            <div class="poll-option">
                                <input type="radio" name="choice" value="<?php echo $choice->choice_id?>" id="c<?php echo $index;?>">
                                <label for="c<?php echo $index;?>"><?php echo $choice->name;?></label>
                            </div>
                        <?php endforeach;?>

                    </div>

                    <input type="submit" value="Send answer">
                    <input type="hidden" name="poll" value="<?php echo $id;?>">
                </form>
                <?php else: ?>
                <p>No available poll</p>

                <a href="add_choices.php?topoll=<?php echo $id; ?>">Add choices &raquo;</a>
            <?php endif;?>
        <?php endif;?>
        </div>
    <?php endif;?>
    </body>
</html>

来自poll.js的删除功能是:

function deletePoll(poll) {
  var confirm = confirm('Are you sure you want to delete this poll?');
  if (confirm) {
    $.ajax({
      url: 'deletePoll.php',
      type: 'get',
      dataType: 'json',
      data: {
        poll: poll
      },
      beforeSend: function() {
        $('.deletion').html('Deleting...');
      },
      success: function(data) {
        setTimeout(function() {
          if (data.deleted) {
            $('.deletion').html('Poll deleted');
          }
        }, 2000);
      }
    });
  }
}

页面的网址如下所示: poll.js?轮询= 1

当我点击“删除投票”时,没有任何反应! 为什么呢?

2 个答案:

答案 0 :(得分:1)

您的问题是您正在呼叫return false; 在你调用deletePoll();函数之前。 所以,deletePoll();函数永远不会被调用。

答案 1 :(得分:0)

在每个包含onclick事件的元素上,您写道:return false。当JavaScript读取文本并在点击事件时评估它时,会立即返回错误值。

<a class="delete" href="#" onclick="return false; deletePoll(<?php echo $id; ?>);">Ștergeți sondajul</a><span class="deletion"></span> | 
<a href="#" onclick="return false; closePoll(<?php echo $id?>)">Închideți sondajul</a><span class="closing"></span>