Javascript jQuery触发器单击(或选择)所选下拉选项

Javascript jQuery触发器单击(或选择)所选下拉选项,javascript,jquery,html,jquery-chosen,Javascript,Jquery,Html,Jquery Chosen,我正试图在页面加载时使用jQuery的下拉选项触发一个click函数,这样后续操作就可以进行了,但是还不能正常工作 我试过: jQuery("document").ready(function() { setTimeout(function() { jQuery("customlist_chzn_o_2").trigger('click'); },10); 或 或 它们都不起作用,请注意,我选择的id是#customlist我需要单击的下拉元素的id是#customlist\u chz

我正试图在页面加载时使用jQuery的下拉选项触发一个click函数,这样后续操作就可以进行了,但是还不能正常工作

我试过:

jQuery("document").ready(function() {
setTimeout(function() {
    jQuery("customlist_chzn_o_2").trigger('click');
},10);


它们都不起作用,请注意,我选择的id是
#customlist
我需要单击的下拉元素的id是
#customlist\u chzn\u o_2
,如果我理解正确,选择选项的值是9,您需要在原始
选择上触发
更改
事件,而不是在自定义或其
选项上触发

使用表单字段时,您通常希望在选择或取消选择值后执行某些行为。每当用户在Selected中选择一个字段时,就会触发原始表单字段上的“更改”事件

但是当更改原始
选择
值时,应触发
选择:更新

如果需要更新选择字段中的选项,并希望选择以获取更改,则需要在该字段上触发“selected:updated”事件。Selected将根据更新的内容重新构建自身

var sel1=$('#sel1')。选择({width:'150px'})。更改(函数(){
log($(this.val());
});
sel1.触发(“变更”);
var sel2=$('#自定义_字段')。选择({width:'150px'});
//$(“按钮”)。单击(函数(){
$(文档).ready(函数(){
sel2.val('9');
//然后您应该触发“已选择:已更新”
sel2.触发器(“已选择:已更新”);
});

选择1
选择2
选择3

选择1 选择2 9
将自定义_字段的值设置为“9”
所选
没有单击/选择的直接触发器。我们必须获取基础
选择元素的所选值,并显式运行所选
的内容。('change',…)

或者,根本不要依赖于所选的更改,而是为原始的
html元素创建一个。我在这里不赘述。

要真正“触发”单击事件,就像它是由用户创建的一样,我必须按如下方式组合多个答案:

        $('#sel1')[0].selectedIndex = 0; // make selection
        $('#sel1')[0].focus(); // set the focus
        $('#sel1').trigger('change'); // actually trigger click event for listeners

您使用的是哪个插件?能否向我们展示您的完整代码?此代码将应用于管理员的“添加产品”视图中virtuemart的“自定义字段”区域。这与位置无关。如果我理解正确,您使用的是自定义下拉列表的插件。不是吗?如果是,请告诉我们是哪一个。如果您可以创建一个或正在使用的插件,效果会更好我需要处理的代码是基于jQuery选择的,这里是:我尝试了这段代码,但它不起作用:
jQuery(document).ready(function(){jQuery('#custom_field').val('9');jQuery('#custom_field')。trigger('selected:updated');})
我将尝试通过点击按钮而不是文档准备来测试代码。PS:我真的很感谢你的帮助。我更新了我的答案。你的代码与此类似吗?我更改了名称和事件,就像你的一样。它确实触发了它,但后续脚本产生了冲突。我必须自己解决。不过非常感谢。
giving conflict
什么意思?另外,如果您发现我的答案对其他用户有帮助,请接受。我认为我需要触发一个单击事件,而不是默认选择值。
jQuery("document").ready(function() {   
    jQuery('#customlist_chzn_o_2').trigger("chosen:updated")    
});
// Original trigger function thru chosen.js
$('#custSelect').on('change', function(evt,params) {
    var custID = params.selected;
    //that gives us the VALUE of the selected option

    // below are the executive actions being done
    if(custID) chooseOne(custID);
});

// Programmatically changing the selected option
var e = document.getElementById('custSelect');
e.selectedIndex +=1 ; // selecting next option in the dropdown list

$('#custSelect').trigger('chosen:updated'); // this is only COSMETIC.
// updates the view to match underlying select element 
// but doesn't trigger chosen's on-change event

// now we retrieve the newly selected option's value:
var custID = e[e.selectedIndex].value; // analogous to params.selected above
// ...and do the execution ourselves.
if(custID) chooseOne(custID);
        $('#sel1')[0].selectedIndex = 0; // make selection
        $('#sel1')[0].focus(); // set the focus
        $('#sel1').trigger('change'); // actually trigger click event for listeners