【问题标题】:Clear dropdown selection on radio button click单击单选按钮清除下拉选择
【发布时间】:2011-02-10 15:52:30
【问题描述】:

我有一组单选按钮。选择主要时,主要公司字段将隐藏。我也想在这个时候清除下拉选择。我该怎么做?

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

【问题讨论】:

    标签: javascript jquery asp.net-mvc drop-down-menu radio-button


    【解决方案1】:

    这是最适合我的捷径:

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

    【讨论】:

    • 对我来说看起来很奇怪 - 这个问题已经有七年了,有多个答案看起来比你的要好得多,这应该是“最好的捷径”?
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:
      $("#primary_company").find('[selected]').removeAttr("selected");
      

      【讨论】:

        【解决方案4】:

        您可以像这样清除选择(即选择第一个选项):

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

        【讨论】:

        • +1。但为什么是.parent('select'); 部分?你可以直接使用.end(),对吧?
        • @David - 这是为邪恶目的服务的较长链条的一部分。 (我从我的一个测试页面粘贴了太多代码),但既然你指出了这一点,endparent('select') 更好。谢谢。
        【解决方案5】:

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

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-09-11
          • 1970-01-01
          相关资源
          最近更新 更多