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

发布于 2024-08-29 21:22:47 字数 772 浏览 3 评论 0原文

我有一组单选按钮。选择主要公司后,主要公司字段将被隐藏。我现在还想清除下拉选择。我该怎么做?

<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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

超可爱的懒熊 2024-09-05 21:22:47

您可以清除选择(即选择第一个选项),如下所示:

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

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

$("#primary_company").find('option:first')
                     .attr('selected','selected');
时光瘦了 2024-09-05 21:22:47

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

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

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

$('#primary_company').val(null);
烟燃烟灭 2024-09-05 21:22:47

要完全清除选择(以便甚至不选择第一个元素),您可以使用:

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

如果 #primary_company 是多选框,您可以使用:

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

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);
纵情客 2024-09-05 21:22:47
$("#primary_company").find('[selected]').removeAttr("selected");
$("#primary_company").find('[selected]').removeAttr("selected");
美男兮 2024-09-05 21:22:47

这是最适合我的捷径:

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

This is the best short way that works for me:

$("#selectNetBankNameId")[0].selectedIndex = 0;
$("#selectNetBankNameId").trigger("change");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文