2015-01-15 75 views
0

我想建立一个比赛的投票应用程序,我不明白为什么onclick函数不工作,我也包括jquery和文件到页面中。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

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

+4

'return false;'将结束内联javascript的执行。取而代之,并从函数返回false。 – Archer

+2

在调用'deletePoll()'之前,你正在做'return false;'以便函数永远不会运行。尝试将它们交换。 – Rhumborl

+0

谢谢,它的作品<3 –

回答

1

您的问题是,在致电deletePoll();函数之前,您正在调用return false; 。 soo,deletePoll();函数永远不会被调用。

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>