ajax this 是,jQuery ajax this.id undefined

2023-05-16

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

I have a list of items I delete using AJAX.

This list is a simple list with divs and each div as an id so when the item is removed from the database I return true and then it removes the line.

Here my code:

HTML

item1
view
delete

JS

$('.delete').click(function () {

if (!confirm('Are you sure you want to delete?')) {

return false;

}

$.ajax({

type: "POST",

url: '/delete_record',

data: 'id=' + this.id,

cache: false,

success: function (result) {

if (result == 'good') {

$('#row' + this.id).remove();

}

}

});

});

For some reason the this.id does not work because this.id is undefined ... why? I have id="1" on my a href.

回答1:

The this you are using refers to the ajax object because there is a new scope within that function. If you want to access variables outwide your scope, you have to declare them before the ajax call.

$('.delete').click(function () {

if (!confirm('Are you sure you want to delete?')) {

return false;

}

var _this = this;

$.ajax({

type: "POST",

url: '/delete_record',

data: 'id=' + this.id,

cache: false,

success: function (result) {

if (result == 'good') {

$('#row' + _this.id).remove();

}

}

});

});

回答2:

Your ID should not start with a number. Here is how to formulate a valid id: What are valid values for the id attribute in HTML?

this within your success callback function does not refer to the this in your click handler function. So you need to make a reference to this in your click handler so it can be accessed in your callback function:

$('.delete').click(function () {

if (!confirm('Are you sure you want to delete?')) {

return false;

}

var that = this;

$.ajax({

type: "POST",

url: '/delete_record',

data: 'id=' + this.id,

cache: false,

success: function (result) {

if (result == 'good') {

$('#row' + that.id).remove();

}

}

});

});

回答3:

$('.delete').click(function () {

if (!confirm('Are you sure you want to delete?')) {

return false;

}

var id = this.id; // here is the magic

$.ajax({

type: "POST",

url: '/delete_record',

data: 'id=' + this.id,

cache: false,

success: function (result) {

if (result == 'good') {

$('#row' + id).remove();

}

}

});

});

回答4:

try to declare before the $.ajax the $this

like:

var $this = this;

and use the variable.

$('.delete').click(function () {

var $this = this;

if (!confirm('Are you sure you want to delete?')) {

return false;

}

$.ajax({

type: "POST",

url: '/delete_record',

data: 'id=' + $this.id,

cache: false,

success: function (result) {

if (result == 'good') {

$('#row' + $yourid).remove();

}

}

});

});

回答5:

I don't see any element with a class of 'delete' in your html. Assuming it's the delete link, you need to get the id with $(this).attr('id') instead of this.id.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ajax this 是,jQuery ajax this.id undefined 的相关文章

  • qTip2 jquery 与 asp.net 文本框

    我怎样才能实现jquery插件qTip2http craigsworks com projects qtip2 http craigsworks com projects qtip2 与 asp net 文本框 鼠标悬停时的优雅工具提示
  • 我可以使用 jQuery 的 AJAX 将表单信息提交到 Google Spreadsheets 吗?

    我喜欢能够使用谷歌电子表格 但以视觉方式集成到我的网站中很棘手 所以我想我会重新创建他们的表单 一个简单的剪切 粘贴 通过 AJAX 提交表单 数据将转到电子表格 我可以将我的用户重定向到比谷歌默认的 谢谢你所做的一切 更漂亮的东西你刚刚做
  • 从json中获取所有子节点

    我有以下 json var source k 01 k 02 children k 05 k 06 children k ABC k PQR k 07 k 03 我希望能够指定 k 的值并取回所有孩子 以及孙
  • jQuery appendTo(), json 在 IE 6,7,8 中不起作用

    我这两天绞尽脑汁想找到解决办法 我使用 jQuery ajax 从数据库中获取值 以便在另一个框发生更改时更新一个框 php 脚本从数据库中获取值 然后输出 json 它在 FF 中工作正常 但在所有版本的 IE 中 选择框都不会更新 我已
  • 将“this”指针传递给析构函数中的其他类/函数

    在某些主对象的析构函数中在堆栈上创建工作对象并传递this主对象指向辅助对象的指针 然后 辅助对象还将调用主对象的成员函数或访问成员变量 换句话说 下面的C 是合法的吗 struct MasterClass MasterClass int
  • 请求 jquery.min.map 文件时出现引用 404 错误

    现在 当浏览器请求 jquery min map 时 我会收到此 404 错误 有趣的是 我从未将此文件添加到我的解决方案中 谁能向我解释如何消除这个错误 我不知道该文件在哪里被引用 因为我没有添加对此文件的引用 Request URL h
  • 实际上并不向服务器提交任何内容的 HTML 表单(JavaScript 中的客户端处理)

    我有一个带有输入的 HTML 表单 它只会在 JavaScript 和 jQuery 中进行客户端处理 但实际上不会向服务器提交任何内容 在 HTML 中布局此类表单并编写 JavaScript 的正确方法是什么 该 JavaScript
  • jQuery 的简单日历网格

    我发现的大多数可爱的日历组件对于我的简单要求来说都太复杂了 我需要显示月份网格 并指示是否已预订个别日期 我不需要每天处理多个活动 等等 我想我可以调整其中一个以始终将预订扩展为全天活动 读者对此有何建议 我的目标是显示住宿加早餐预订的日期
  • 带有输入的动态创建行上的日期选择器

    我有一个表单可以使用输入动态创建新行 每个新行上的日期输入应该有一个日期选择器 我几乎可以正常工作 但是当创建带有输入的新行时 日期选择器将不再在已经存在的日期字段上工作 我玩了一整天来找出我做错了什么 但我就是不知道如何解决这个问题 这是
  • 使用智能菜单jquery打印json

    menu name Computers children name Notebook children name Apple name Windows name Tablets children name Apple name Androi
  • ScrollTop 在 Chrome/Safari 中不起作用

    我的网站上有一个循环内的表单 当有人提交表单时 查询字符串会添加到 URL 中 例如 updated 111 然后 我的 JQuery 脚本检查数字的 url 并在提交表单并重新加载页面后滚动到该 div 该脚本在 Firefox 中运行良
  • 如何将 jquery 应用于具有相同 id 属性的所有元素?

    如何将 jquery 应用于具有相同 id 属性的所有元素 我想申请focus and blur 函数在一个textarea具有相同 id 的元素 正确的答案是 id yourID doSomething 对于任何代码 例如
  • 从对象获取数据 - 我看到数据但无法保存它们

    正如你所看到的 我是新来的 我确实尝试过搜索 但没有找到解决我问题的方法 所以这是我的问题 如果我这样做 console log grid data kendoGrid data 这在控制台中显示如下 所以我明白这一点 有一个数组和一个带有
  • jQuery 时间戳之前的时间?

    下面是一个非常好的 jQuery 插件 与他们在 SO 上使用的插件非常相似 对我来说问题是它用它来转换时间
  • jQuery Masonry 和 CSS3

    如何在保持砖石布局的同时将 CSS3 过渡与当前图像结合起来 这是我的html div img class bottom src images div
  • 'DOMException:使用'option:selected'选择器时无法在'Element'上执行'querySelectorAll'

    我正在运行一个页面 该页面在以下行中引发错误 var label select find option selected html select find option first html 为了完整起见 这里是完整的 jQuery 函数
  • 加载 angularjs 路由后运行 javascript 代码

    我需要在 angularjs 加载路线后显示警报 显示警报的代码位于 angularjs 异步加载的视图中 视图加载后 我希望它能够运行 但它没有 我知道我可以广播并告诉它稍后运行等 但我需要一个更通用的解决方案 假设您正在谈论基于以下内容
  • jQuery 单击附加元素不起作用

    我有一个数组 我正在从 Array 获取数据并在 jQuery Append to list 中使用它 但是当我单击列表项时 它只显示最后一个元素 var array 1 2 7 3 4 8 5 6 9 for var i 0 i lt a
  • phonegap:基于 cookie 的身份验证 (PHP) 不起作用 [webview]

    我正在开发一个移动网络应用程序 使用 sencha touch HTML5 和 Phonegap 作为包装器 我正在使用 PHP 身份验证 Cookie 和 ajax 请求 在 safari 或 chrome 上一切正常 但在使用phone
  • 处理延迟对象数组

    自从使用 Deferred我已经遇到过这种情况几次 我有一个值列表 每个值都以某种方式生成一个延迟对象 并且我想在所有延迟对象都解析后执行回调 一个更具体的例子是这样的 var urls foo com bar com baz com qu

随机推荐