[英]preventDefault is not working
I am trying to send an ajax request to complete a task when a link is clicked. 单击链接时,我试图发送ajax请求以完成任务。 For some reason the
e.preventDefault
is not working and I still get sent to the page. 由于某些原因,
e.preventDefault
无法正常工作,我仍然被发送到该页面。 Is there something I am missing? 我有什么想念的吗?
It is simply supposed to go to the page set the check-box to checked (or unchecked) then redirect. 只需将页面设置为选中(或未选中)复选框然后重定向即可。 But again it just goes to the page.
但是,它再次进入页面。 Any help would be great!
任何帮助将是巨大的!
here is the main.js
这是
main.js
$(function() {
$('.ajax-task-complete').on({
click: function(e) {
e.stopPropagation();
e.preventDefault();
var href = $(this).attr('href');
$('<div></div>').load(href+' form', function() {
// Set Form
var form = $(this).children('form');
// Set Checkbox
var cb = form.find('input[type="checkbox"]');
// Taggle Checkbox
cb.prop('checked', !cb.prop('checked'));
// Form Action URL
var url = form.attr('action');
// Set Data
var data = form.serialize();
$.ajax({
url: url,
data: data,
method: 'post',
dataType: 'json',
cache: false,
success: function(obj) {
var tic = $('#task-complete-' + obj.id + ' .ajax-task-complete');
},
complete: function() {
console.log('complete');
},
error: function(err) {
console.log(err);
}
});
});
}
});
});
here is the requested button 这是要求的按钮
<td id="task-complete-{{ entity.id }}">
{% if entity.completed %}
<a class="check ajax-task-complete" href="{{ path('task_edit_complete', { 'id': entity.id }) }}">✔</a>
{% else %}
<a class="uncheck ajax-task-complete" href="{{ path('task_edit_complete', { 'id': entity.id }) }}">✔</a>
{% endif %}
</td>
This is your problem: 这是你的问题:
Uncaught ReferenceError: $ is not defined?
未捕获ReferenceError:未定义$?
Read HERE from SO how you load your jQuery-plugin the right way. 从这里了解如何以正确的方式加载jQuery插件。
you might need to stop bubbling depending on where the click event is occurring. 您可能需要停止鼓泡,具体取决于点击事件发生的位置。 Try adding this and see what happens.
尝试添加它,看看会发生什么。
e.stopPropagation();
Your scope is wrong, you are missing the ()
after (function)()
. 你的范围是错的,你错过了
()
后(function)()
The function is never called : 该函数从不调用:
(function() {
$('.ajax-task-complete').on({
click: function(e) {
e.stopPropagation();
e.preventDefault();
var href = $(this).attr('href');
$('<div></div>').load(href+' form', function() {
// Set Form
var form = $(this).children('form');
// Set Checkbox
var cb = form.find('input[type="checkbox"]');
// Taggle Checkbox
cb.prop('checked', !cb.prop('checked'));
// Form Action URL
var url = form.attr('action');
// Set Data
var data = form.serialize();
$.ajax({
url: url,
data: data,
method: 'post',
dataType: 'json',
cache: false,
success: function(obj) {
var tic = $('#task-complete-' + obj.id + ' .ajax-task-complete');
},
complete: function() {
console.log('complete');
},
error: function(err) {
console.log(err);
}
});
});
}
});
})(); //Add () here
But is the scope really needed here? 但是这里真的需要范围吗?
Maybe you meant a ready handler, then it should be like this : 也许您的意思是准备好处理程序,那么它应该像这样:
$(function); //Missing '$'
**"$"**
is missing before (function() {
在
(function() {
javascript pageload event should be like javascript pageload事件应类似于
$(function(){
//code here
});
or function will be called by themself 或功能将由他们自己调用
(function(){
//code here
)}();
or 要么
(function($){
//code here
})(jQuery)
Try placing the preventDefault event at the end of the function as below 尝试将preventDefault事件放在函数的末尾,如下所示
$('.ajax-task-complete').on({
click: function(e) {
//
//your code here then,
e.preventDefault();
e.stopPropragation();
});
I can't test if it works or not, as I'm on mobile. 我无法在手机上测试它是否有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.