Jquery ange事件(如重新加载表单)。否则,在触发change()时将冗余调用这些操作。如果此操作无法正常工作,请添加setTimeout:setTimeout(function(){$(“select”).val(“1”).trigger(“change”);

Jquery ange事件(如重新加载表单)。否则,在触发change()时将冗余调用这些操作。如果此操作无法正常工作,请添加setTimeout:setTimeout(function(){$(“select”).val(“1”).trigger(“change”);,jquery,jqgrid,ui-select2,Jquery,Jqgrid,Ui Select2,ange事件(如重新加载表单)。否则,在触发change()时将冗余调用这些操作。如果此操作无法正常工作,请添加setTimeout:setTimeout(function(){$(“select”).val(“1”).trigger(“change”);},1000)@我也有同样的问题。理想情况下,我希望在不触发事件的情况下更改显示为选中的内容(因为我有一个自定义事件绑定到onChange),请参见下面我的答案,了解如何解决@vanu folmert问题的示例如果在方括号下输入字符串,这将不起


ange事件(如重新加载表单)。否则,在触发
change()
时将冗余调用这些操作。如果此操作无法正常工作,请添加setTimeout:setTimeout(function(){$(“select”).val(“1”).trigger(“change”);},1000)@我也有同样的问题。理想情况下,我希望在不触发事件的情况下更改显示为选中的内容(因为我有一个自定义事件绑定到
onChange
),请参见下面我的答案,了解如何解决@vanu folmert问题的示例如果在方括号下输入字符串,这将不起作用。我发现这比处理select2“数据”更干净:)更改dom,我正在Safari上进行测试,这是唯一有效的解决方案。其他人更雄辩,我更喜欢他们,但最重要的是,我更喜欢工作代码。这就是我想要的。:)万分感谢!我正在使用select2框,其中两个框都使用Ajax填充,我使用额外的数据属性为第一个框设置第二个框的值。再次感谢大家!!!!是的,这是最好的全面解决方案。实际上,我认为Select2应该触发内部的“change.Select2”事件,以便在值更改时自动更新显示。但是,这至少提供了一种更新显示以匹配新值的简单方法,而无需触发自定义更改事件处理程序为我工作。谢谢@minlare。虽然这段代码可能会回答这个问题,但提供关于这段代码为什么和/或如何回答这个问题的附加上下文可以提高其长期价值。我不了解JqGrid,但通过这段代码,您可以用简单的方式更改select2值尝试触发('change.select2')。触发('select2:select');如果仅仅改变是不够的
$("#select").select2("val"); //get the value
$("#select").select2("val", "CA"); //set the value
$("#s2id_originalSelectId").select2("val", "value to select");
this.$("#yourSelector").select2("data", { id: 1, text: "Some Text" });
$("#e1").select2("val", ["View" ,"Modify"]);
$('select').val('1').trigger('change');
$('select').val('1').trigger('change.select2');
$('#my-best-friend').select2();
$('#my-best-friend').val('').trigger('change');
// I have assume you can write code to select the next guy in the list
$('#my-best-friend').val('Bill').trigger('change');  
$('select').val('1').trigger('change'); // instead of $('select').select2('val', '1');
$('#my-select').val('').change();
var opt = "<option value='newValue' selected ='selected'>" + newLabel+ " </option>";
$(selLocationID).html(opt);
$(selLocationID).val('newValue').trigger("change");
$('#select').val("string1").change(); 
$('#select').val("E").change(); // you must enter the Value instead of the string
$(element).val(val).trigger('change');
$(element).val(val).trigger('change.select2');
var intValueOfFruit = 1;
var selectOption = new Option("Fruit", intValueOfFruit, true, true);
$('#select').append(selectOption).trigger('change');
$('#mySelect2').select2({
    ajax: {
        url: '/api/students'
    }
});
var studentSelect = $('#mySelect2');
$.ajax({
    type: 'GET',
    url: '/api/students/s/' + studentId
}).then(function (data) {
    // create the option and append to Select2
    var option = new Option(data.full_name, data.id, true, true);
    studentSelect.append(option).trigger('change');

    // manually trigger the `select2:select` event
    studentSelect.trigger({
        type: 'select2:select',
        params: {
            data: data
        }
    });
});
 $('select#id').val(selectYear).select2();
// Set up the Select2 control
$('#mySelect2').select2({
    ajax: {
        url: '/api/students'
    }
});

// Fetch the preselected item, and add to the control
var studentSelect = $('#mySelect2');
$.ajax({
    type: 'GET',
    url: '/api/students/s/' + studentId
}).then(function (data) {
    // create the option and append to Select2
    var option = new Option(data.full_name, data.id, true, true);
    studentSelect.append(option).trigger('change');

    // manually trigger the `select2:select` event
    studentSelect.trigger({
        type: 'select2:select',
        params: {
            data: data
        }
    });
});
        var timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
        var $select2 = $('#time-zone');
        var selected = $select2.find("option:contains('"+timeZone+"')").val();
        $select2.val(selected).trigger('change.select2'); 
$("#select").select2("val"); //get the value
$("#select").select2("val", "CA"); //set the value
$('#select').val("E").change(); // you must enter the Value instead of the string