简体   繁体   English

使用 ajax 从 select2 更改所选项目

[英]Change selected item from select2 with ajax

I want to use jquery to select the value of the desired item from select2.When I manually write the following code, it works correctly.我想使用 jquery 到 select select2 中所需项目的值。当我手动编写以下代码时,它可以正常工作。

<!-- language: lang-html -->

<select class="form-control select2" id="voip">
    <option value="101001001">A</option>
    <option value="101001002">B</option>
    <option value="101001003">C</option>
    <option value="101001004">D</option>
</select>

$('#voip').val('101001003').trigger('change');

But when I receive the desired value with Ajax, it does not work.但是当我收到 Ajax 所需的值时,它不起作用。 Please guide me, thank you请指导我,谢谢

 $.ajax({
    url:"ajax_voip.php?internal=1",
    method:"GET",
    success:function(data)
    {
        $('#voip').val(data).trigger('change'); 
        alert(data);//this ok return 101001003
     }
})

it's possible your ajax call is returning a number and not a string... try:您的 ajax 调用可能返回数字而不是字符串...尝试:

$('#voip').val((""+data).trim()).trigger('change'); 

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

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