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');
You can pass null to the jQuery val()
method as a nice way to clear a dropdown.
$('#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 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");
精彩评论