【问题标题】:jquery $(this).attr('id') is giving undefined result [duplicate]jquery $(this).attr('id') 给出未定义的结果[重复]
【发布时间】:2014-05-30 06:19:03
【问题描述】:

我正在尝试获取更改元素的 id 的详细信息。 但是当我试图使用下面的代码找到它时,它给出了错误。

HTML:

<input type="text" required="required" placeholder="Enter city name" class="get_neighborhood" maxlength="30" id="city" value="P" name="city" data-invalid="">
<input type="text" placeholder="State code" maxlength="2" class="get_neighborhood" required="required" id="state" value="AA" name="state">

JS:

$(document).ready(function() {
    $(".get_neighborhood").on('change',function() {
        city = $('#city').val();
        ajax_url = 'alternate_address/ajax_get_country/';

        $.ajax({
            type: 'POST', // or 'POST', whatever you want.
            dataType: 'json', // output_value will be a plain text string.
            data: {
                city: city
            },
            url: ajax_url,
            beforeSend: function(msg) {                

            },
            success: function(output_value) {
                myid = $(this).attr('id');                
                //var id=this.id;
                console.log(myid.toSource());

            },
            error: function(output_value) {
                alert('err');                    
            }
        });
    });
});

这给出了 myid 未定义的错误。 当我更改城市或州字段时。

【问题讨论】:

  • 这是指你的成功函数
  • 我不明白为什么要给 -1 或这个问题。 ???
  • 感谢大家的快速回复。

标签: javascript jquery


【解决方案1】:

尝试在该 ajax 调用之外缓存 $(this) 引用并在其中使用它。

完整代码:

$(".get_neighborhood").on('change',function() {

        city = $('#city').val();
        ajax_url = 'alternate_address/ajax_get_country/';
        _this = $(this);

        $.ajax({
            type: 'POST', // or 'POST', whatever you want.
            dataType: 'json', // output_value will be a plain text string.
            data: {
                city: city
            },
            url: ajax_url,
            beforeSend: function(msg) {                

            },
            success: function(output_value) {
                myid = _this.attr('id');                
                //var id=this.id;
                console.log(myid.toSource());

            },
            error: function(output_value) {
                alert('err');                    
            }
        });

});

【讨论】:

  • 所有答案都是一样的......但你先写了。谢谢,:)
  • @TechCare99 很高兴为您提供帮助...!
【解决方案2】:

在 ajax 中,您会丢失元素事件上下文,因此您无法在 ajax 中访问它,您需要做的是将事件后的 id 存储在变量中并在 scucess 中使用它:

$(".get_neighborhood").on('change',function() {
        city = $('#city').val();
     var Id = this.id;

     //or

    var element = $(this);

......

.....

success: function(output_value) {
                myid = Id;            

}

或存储元素引用并像这样使用:

$(".get_neighborhood").on('change',function() {

        var element = $(this);

    ......

    .....

    success: function(output_value) {
                    myid = $(element).attr("id");            

    }

【讨论】:

  • 谢谢你.... ehsan aapka :)
【解决方案3】:

您在错误的上下文中使用了 $(this)。将事件源存储在某个变量中并成功使用。

$(document).ready(function() {
    $(".get_neighborhood").on('change',function() {
        source = $(this); //Store $(this);
        city = $('#city').val();
        ajax_url = 'alternate_address/ajax_get_country/';

        $.ajax({
            type: 'POST', // or 'POST', whatever you want.
            dataType: 'json', // output_value will be a plain text string.
            data: {
                city: city
            },
            url: ajax_url,
            beforeSend: function(msg) {                

            },
            success: function(output_value) {
                myid = source.attr('id'); //Use source here.                
                //var id=this.id; 
                console.log(myid.toSource());

            },
            error: function(output_value) {
                alert('err');                    
            }
        });
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 2014-10-02
    • 2015-09-06
    • 2014-01-19
    • 2015-12-02
    • 1970-01-01
    • 2012-05-21
    相关资源
    最近更新 更多