当我加载页面时,document.on('click',...)
无论条件如何,请求都会立即发送
例子:
$(document).ready(function(){
var poll = new Poll();
if($('.loadPoll')[0]){ //this works fine
poll.loadPoll(function(){
console.log("loaded poll successful");
});
}
$(document).on('click', '#SubmitVote', poll.afterSubmit(function(){
console.log("Poll has been voted on!");
})); //this gets sent regardless of clicks
});
即使没有带有SubmitVote
ID 的元素,它也会自动发送
编辑:
变成:
$(document).ready(function(){
var poll = new Poll();
if($('.loadPoll')[0]){
poll.loadPoll(function(){
console.log("loaded poll successful");
});
}
$(document).on('click', 'button.SubmitVote', function(){
poll.afterSubmit(function(){
console.log("Poll has been voted on!");
});
});
}