打开 bootstrap 模式后如何将下拉列表重置为默认值

how to reset dropdownlist to default after open the bootstrap modal

我想要做的是,当用户从下拉列表中打开模式和 select 选项并关闭它而不做任何事情并重新打开它时,下拉列表变为“--select-- " 再次

<pre>
<div class="inpu_modal">
@Html.LabelFor(m => m.Add_interaction.with_name, new { @class = "sub-title alighn" })
@Html.DropDownListFor(m => m.Add_interaction.with_name, new SelectList(ViewBag.names, "Value", "Text"), "--Select--", new { @class = "with", @autocomplete = "off" })
<span class="field-validation-valid text-danger with_name_span"></span>
</div>
</pre>



function Open_new_interaction() {
        document.querySelector('.drug_name_span').innerHTML = null;
        document.querySelector('.with_name_span').innerHTML = null;
        document.querySelector('.Degree_span').innerHTML = null;
        $('.drug option:selected').removeAttr('selected');
        $('.with option:selected').removeAttr('selected');
        $('#Add_InteractionDrug').modal("show");
    }

由于您使用的是 bootstrap 模式,因此您必须这样做:

$( ".modal-class" ).on('shown', function(){
    //reset the  dropdown here, do all the reset logic here
    //check if select dropdown is not null, and then loop through and get the default and set the selected.
    //
   let dropDown = document.querySelector("#dropdownId");
   //if the default value is at index 0, then just
    dropDown.selectedIndex = 0;
   /*if not, then loop through the options and find the option by inner HTML(i forget 
    if it contains a value*/  
    //if it has a value)

});