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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题