dqyp50298 2014-09-25 13:16
浏览 40
已采纳

使用基于按钮单击的Jquery填充选择下拉列表

I have 2 buttons and a select dropdown list

<button type="button" name="btnmonth" id="btnmonth">Monthly</button>
<button type="button" name="btnyear" id="btnyear" >Yearly</button>

<select id="subscriptions">
    <option value="default">Please Choose a Plan</option>
</select>

On the button click of month i need the select drop down list populated with 4 choices. Or the click of year the list needs to be populated with the 4 year choices. But if you go back and forth between month and year i need the list to not have 1000 of the same choices.

I used jquery .show and .hide on two different selects but the form was submitting both of them even if the year select was hidden so I didnt know which was actually chosen. So it has to be one select drop down

  • 写回答

3条回答 默认 最新

  • duanjin9035 2014-09-25 13:25
    关注

    You can use two selects as you tried, but just .hide() does not remove it from the form. To remove it from form you can disable it, using $.prop('disabled', boolean)

    <button type="button" name="btnmonth" id="btnmonth">Monthly</button>
    <button type="button" name="btnyear" id="btnyear" >Yearly</button>
    
    <select id="subscriptions-monthly" style="display: none" disabled>
        <!-- Monthly select options -->
    </select>
    
    <select id="subscriptions-yearly" style="display: none" disabled>
        <!-- Monthly select options -->
    </select>
    

    In javascript:

    $('#btnmonth').click(function(event) {
        $("#subscriptions-yearly").hide().prop('disabled', true);
        $("#subscriptions-monthly").show().prop('disabled', false);
    });
    
    $('#btnmonth').click(function(event) {
        $("#subscriptions-monthly").hide().prop('disabled', true);
        $("#subscriptions-yearly").show().prop('disabled', false);
    });
    

    This should work to you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'