Onclick 在 jquery 中不工作
Onclick isn't working in 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>✓ 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 »</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);
}
});
}
}
页面的 url 如下所示:
poll.js?投票=1
当我点击 "Delete Poll" 时没有任何反应!
为什么?
你的问题是你正在调用 return false;
在调用 deletePoll();
函数之前。
所以,deletePoll();
函数永远不会被调用。
在具有 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>
我想为比赛构建一个投票应用程序,但我不明白为什么 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>✓ 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 »</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);
}
});
}
}
页面的 url 如下所示: poll.js?投票=1
当我点击 "Delete Poll" 时没有任何反应! 为什么?
你的问题是你正在调用 return false;
在调用 deletePoll();
函数之前。
所以,deletePoll();
函数永远不会被调用。
在具有 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>