2015-01-15 108 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>