Javascript 设置选择选项';选定的';在模态引导中

Javascript 设置选择选项';选定的';在模态引导中,javascript,jquery,html,twitter-bootstrap,Javascript,Jquery,Html,Twitter Bootstrap,我有一个选择字段,我必须用jQuery选择其中一个选项。我有这个方法,它在任何地方都适用,除了在模态中。 当我打开我的目标页面时,它会自动打开一个模式,其中有一个选择字段,我正试图设置他的所选项目 我有以下HTML: <div class='modal small fade' id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div

我有一个选择字段,我必须用jQuery选择其中一个选项。我有这个方法,它在任何地方都适用,除了在模态中。
当我打开我的目标页面时,它会自动打开一个模式,其中有一个选择字段,我正试图设置他的所选项目

我有以下HTML:

<div class='modal small fade' id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form method="post" action="UpdateMPrev" >
                <select id="echelle"  name="echelle" class="form-control">                     
                    <option value="mensuelle"> Mensuelle</option>  
                    <option value="trimestrielle">Trimestrielle</option> 
                    <option value="semestrielle" > Semestrielle</option> 
                    <option value="annuelle" > Annuelle</option> 
                    <option value="aucun"> Aucun</option> 
                </select>
                <input type="submit"/>
            </form>
        </div>
    </div>
</div> 

门苏埃尔
特里梅斯特莱尔
塞梅斯特莱尔
安奈尔
奥村
然后我就有了这个脚本:

<script>
    var echelle= document.getElementById('echelle');
    echelle.value = 'trimestrielle';
</script>

var echelle=document.getElementById('echelle');
echelle.value='trimestrielle';

相同的代码,在相同的页面工作,只有当我把选择字段放在主页上,我的意思是不在模式中,任何人都可以帮助吗?

您应该将脚本放在模式的html代码之前,如下所示

<script>
    var echelle= document.getElementById('echelle');
    echelle.value = 'trimestrielle';
</script>

<div class='modal small fade' id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form method="post" action="UpdateMPrev" >
                <select id="echelle"  name="echelle" class="form-control">                     
                    <option value="mensuelle"> Mensuelle</option>  
                    <option value="trimestrielle">Trimestrielle</option> 
                    <option value="semestrielle" > Semestrielle</option> 
                    <option value="annuelle" > Annuelle</option> 
                    <option value="aucun"> Aucun</option> 
                </select>
                <input type="submit"/>
            </form>
        </div>
    </div>
</div> 

var echelle=document.getElementById('echelle');
echelle.value='trimestrielle';
门苏埃尔
特里梅斯特莱尔
塞梅斯特莱尔
安奈尔
奥村

是否重复使用
ID
s?该脚本何时运行?显示模式后?