Jquery $(this).prop('selectedIndex', num) 不工作

Jquery $(this).prop('selectedIndex', num) not working

上面提到的代码行通常可以工作,但当我将它从我的 php 脚本移入函数(数据)return 时就不行了。我假设某些事情超出了范围或类似的事情?这是代码,精简到基础。

$('.training_staff_change').on('change',
    function () {

        // Post the data.
        $.post('updatetrainingstaff.php',
            {
            } ,
        function ( data ) {
            $(this).prop('selectedIndex', 5);   
        }
        )
});

正如我所说,这已经精简,其余代码工作正常。如果我将该行移出函数并移到 ) 下方,它可以正常工作并根据需要设置选择器。 $(this) 超出范围了吗?在我的最终代码中,它将 selectedIndex 更改为 'data' return 值,但这不是这里的问题。它甚至不会将其更改为直接的“5”。

是的,这超出了范围(你自己回答你的问题),但要解决这个问题有很多方法,最简单的方法是:

$('.training_staff_change').on('change',
    function () {
        var that = $(this);
        // Post the data.
        $.post('updatetrainingstaff.php',
            {
            } ,
        function ( data ) {
            that.prop('selectedIndex', 5);   
        }
    )
});