Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery$(this.attr(';id';)给出了未定义的结果_Javascript_Jquery - Fatal编程技术网

Javascript jquery$(this.attr(';id';)给出了未定义的结果

Javascript jquery$(this.attr(';id';)给出了未定义的结果,javascript,jquery,Javascript,Jquery,我正在尝试获取已更改元素的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

我正在尝试获取已更改元素的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">
$(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');                    
            }
        });
    });
});
$(".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');                    
            }
        });

});
这会导致myid未定义的错误。 当我更改city或state字段时。

尝试在ajax调用外部缓存
$(this)
引用,并在其中使用它

完整代码:

<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">
$(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');                    
            }
        });
    });
});
$(".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');                    
            }
        });

});

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

$(".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");            

    }
您在错误的上下文中使用了$(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');                    
            }
        });
    });
});

这是指你的成功函数,我不明白为什么给-1或这个问题???谢谢大家的快速回复。所有答案都一样。。。。但是你先写的。谢谢,:)@TechCare99很高兴能帮助。。。!非常感谢。埃桑·阿普卡:)