Javascript 使用多个select设置选定字段的值

Javascript 使用多个select设置选定字段的值,javascript,pug,jquery-chosen,Javascript,Pug,Jquery Chosen,我在Jade模板中使用selected.js来显示所选值的选择框,但是有文档记录的设置值的方法对我不起作用 翡翠模板: select(name='interests', id='interest-input', multiple=true) each interest in interests option(value=interest._id)!=interest.name Javascript: $(

我在Jade模板中使用selected.js来显示所选值的选择框,但是有文档记录的设置值的方法对我不起作用

翡翠模板:

 select(name='interests', id='interest-input', multiple=true)
                    each interest in interests
                        option(value=interest._id)!=interest.name
Javascript:

$(function(){
        $('#interest-input').on('chosen:ready', function(ev, params){
            var myInterests = !{JSON.stringify(profile.interests)};
            $('#interest-input').val(myInterests);
            $('#interest-input').trigger("chosen:updated");
        });
        $('#interest-input').chosen({
            width: "100%", 
            search_contains: true, 
            no_results_text: 'No results found!', 
            placeholder_text_multiple: 'Search...'});
    });
在执行Javascript时已经设置了Profile.interests和interests。我还试着在选项children op interest input on selected上循环:准备检查这些选项在代码执行时是否存在,它们是否存在


如果我只添加带有所选属性集的静态选项,效果很好。

您的问题是什么?这一行看起来不对
var myInterests=!{JSON.stringify(profile.interests)}您无法在运行时访问jade语法或jade变量,这些语法或变量仅在jade的编译时可用!不过那部分是有效的。如果我调试,myInterests将正确填充profile.interests中的内容。唯一不起作用的是将所选组件的value属性设置为myInterests。