Clear dropdown selection on radio button click

倾然丶 夕夏残阳落幕 提交于 2019-12-22 05:52:07

问题


I have a set of radio buttons. When primary is selected, the Primary Company field is hidden. I would also like to at this time clear the drop down selection. How can I do this?

<p>
        <label>Company Type:</label>
        <label for="primary"><input onclick="javascript: $('#sec').hide('slow');" type="radio" runat="server" name="companyType" id="primary" checked />Primary</label>
        <label for="secondary"><input onclick="javascript: $('#sec').show('slow');" type="radio" runat="server" name="companyType" id="secondary" />Secondary</label>
        <div id="sec">
        <label for="primary_company">Primary Company:</label>
            <%= Html.DropDownList("primary_company", Model.SelectPrimaryCompanies, "** Select Primary Company **") %>
        </div>

    </p>

回答1:


You can clear the selection (that is, select the first option) like so:

$("#primary_company").find('option:first')
                     .attr('selected','selected');



回答2:


You can pass null to the jQuery val() method as a nice way to clear a dropdown.

$('#primary_company').val(null);



回答3:


To clear the selection completely (so that not even the first element is selected) you could use:

$('#primary_company').attr('selectedIndex', '-1'); 

If #primary_company is a multiple select box you could use:

$('#primary_company option').attr('selected', false);



回答4:


$("#primary_company").find('[selected]').removeAttr("selected");



回答5:


This is the best short way that works for me:

$("#selectNetBankNameId")[0].selectedIndex = 0;
$("#selectNetBankNameId").trigger("change");


来源:https://stackoverflow.com/questions/2684067/clear-dropdown-selection-on-radio-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!