简体   繁体   English

jQuery在Ajax成功回调中被忽略

[英]jQuery ignored in ajax success callback

I'm using jQuery and Ajax to populate a select. 我正在使用jQuery和Ajax来填充选择。 Now I'm trying to do that in my success callback, But it's being ignored 现在我正在尝试在成功回调中执行此操作,但是它被忽略了

The weird part is that my console.log('test'); 奇怪的是我的console.log('test'); is working in my success callback, But my DOM manipulation isn't. 在我的成功回调中起作用,但是我的DOM操作却没有。 And my DOM manipulation is working outside of the success callback. 而且我的DOM操作在成功回调之外进行。

seems like magic to me. 对我来说似乎很神奇。 Here's the code 这是代码

$(document).ready(function(){

// This is working
//$('select[name="service"]').append('<option selected="selected" value="1">Test</option>');

$('select[name="region"]').change(function(){
    $.ajax({
        type: "GET",
        url: "ajax/updateServices.php",
        data: "region=" + $(this).val(),
        dataType: "html",
        success: function(data){
            // this is also working
            console.log('test');
            // This is being ignored
            $('select[name="service"]').append('<option selected="selected" value="1">Test</option>');

        }
    });
});

}); });

EDIT 编辑

It seems like selectize.js is causing the issue. 似乎selectize.js引起了问题。 I'm gonna look into why selectize.js is causing this. 我要研究为什么selectize.js导致了这一点。

i would share my history with select plugin: Some select plugin such as SelectBox requires you do destroy the jquery object before populating the select again or changing anything, even selectedIndex THAT you need to declare the select as the select-plugin again for example (with selectBox, but works the same on others) 我会与select插件共享我的历史记录:一些Select插件,例如SelectBox,要求您在再次填充select或更改任何内容之前确实销毁了jquery对象,甚至selectedIndex都需要再次将select声明为select插件(例如selectBox,但在其他方面相同)

$("#MySelect").selectBox('destroy')
$("#MySelect").append("<option>New Option </option>");
$("#MySelect").selectBox()

also, make sure when debugging, that $('select[name="service"]') returns the desired select and not an empty object, most issues are resolved when in debug mode (dont use debug mode? use Chrome Developer tools under Source tab). 另外,请确保在调试时确保$('select[name="service"]')返回所需的select而不是空对象,在调试模式下(不使用调试模式?请使用Chrome Developer工具在来源标签)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM