简体   繁体   English

单击单选按钮时清除下拉选择

[英]Clear dropdown selection on radio button click

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>

You can clear the selection (that is, select the first option) like so: 您可以清除选择(即,选择第一个选项),如下所示:

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

您可以将null传递给jQuery val()方法,作为清除下拉列表的好方法。

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

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是一个多选框,您可以使用:

$('#primary_company option').attr('selected', false);
$("#primary_company").find('[selected]').removeAttr("selected");

This is the best short way that works for me: 这是对我有用的最好的简短方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM