jQuery检索元素时出现问题';身份证
在下面的代码中,我需要获取引发事件的元素的IDjQuery检索元素时出现问题';身份证,jquery,Jquery,在下面的代码中,我需要获取引发事件的元素的ID $(document).ready(function () { $(".selectors").live('change', function () { $.post("GetCategoriesByParentId/", { ID: $(this).val() }, function (data) { var idd = $(this).attr('id'); //he
$(document).ready(function ()
{
$(".selectors").live('change', function ()
{
$.post("GetCategoriesByParentId/", { ID: $(this).val() }, function (data)
{
var idd = $(this).attr('id'); //here
});
});
});
但是
idd
总是“未定义”。为什么?在$.post
回调的上下文中,this
的值将设置为与live
调用不同的值。您需要缓存此的值:
$(document).ready(function ()
{
$(".selectors").live('change', function ()
{
var idd = this.id;
$.post("GetCategoriesByParentId/", { ID: $(this).val() }, function (data)
{
// idd is now the id of the changed element
});
});
});
在
$.post
回调的上下文中,this
的值将设置为与live
调用不同的值。您需要缓存此的值:
$(document).ready(function ()
{
$(".selectors").live('change', function ()
{
var idd = this.id;
$.post("GetCategoriesByParentId/", { ID: $(this).val() }, function (data)
{
// idd is now the id of the changed element
});
});
});
$(this)
函数中的.post
实际上不是父循环中要迭代的集合中的当前元素。修正:
$(".selectors").live('change', function ()
{
$thisElement = $(this);
$.post("GetCategoriesByParentId/", { ID: $(this).val() }, function (data)
{
var idd = $thisElement.attr('id'); //here
});
});
$(this)
函数中的.post
实际上不是父循环中要迭代的集合中的当前元素。修正:
$(".selectors").live('change', function ()
{
$thisElement = $(this);
$.post("GetCategoriesByParentId/", { ID: $(this).val() }, function (data)
{
var idd = $thisElement.attr('id'); //here
});
});
更改事件绑定到的HTML会很有用。在该上下文/范围中,这不是post对象吗?更改事件绑定到的HTML会很有用。在该上下文/范围中,这不是post对象吗?