简体   繁体   English

单击单选按钮时如何显示下拉列表?

[英]How to display dropdown list when clicking radio button?

I've tried to call this function but it don't seem to work. 我试图调用此函数,但它似乎不起作用。 Can anyone help me to see if what is wrong with my codes? 谁能帮我看看我的密码有什么问题吗? Any help will be greatly appreciated. 任何帮助将不胜感激。

This is my code for my radio button: 这是我的单选按钮代码:

    <input type="radio" name="radioFrequency" onclick="myMth()" id="radioFrequency-0" value="Monthly (once a month)" />

This is my code for my dropdownlist 这是我的下拉列表的代码

    <div class="control-group" <%--style="display:none;"--%>>
  <label class="control-label" for="ddlMth">Withdrawal Day</label>
  <div class="controls">
    <select id="ddlMth" name="ddlMth" class="input-large">
      <option>Select day</option>
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
      <option>5</option>
      <option>6</option>
      <option>7</option>
      <option>8</option>
      <option>9</option>
      <option>10</option>
      <option>11</option>
      <option>12</option>
      <option>13</option>
      <option>14</option>
      <option>15</option>
      <option>16</option>
      <option>17</option>
      <option>18</option>
      <option>19</option>
      <option>20</option>
      <option>21</option>
      <option>22</option>
      <option>23</option>
      <option>24</option>
      <option>25</option>
      <option>26</option>
      <option>27</option>
      <option>28</option>
      <option>29</option>
      <option>30</option>
      <option>31</option>
    </select>
  </div>
</div>

This is my js code to call the function: 这是我调用该函数的js代码:

<script>
        function myMth() {
            $("input[type='radio']").change(function() {

                if ($(this).val() == "Monthly (once a month)") {
                    $("#ddlMth").show();
                }
        else {
                    $("#ddlMth").hide();
        }
            });
            }

    </script>

Try This Script. 试试这个脚本。

$(document).on("change","#wrapper input[type='radio']",function(){
    if($(this).find("input[type='radio']").prop("checked")){
        //Your Code Here
    }
});

No need of of myMth() just use this: 不需要myMth()只需使用以下代码:

<input type="radio" name="radioFrequency" id="radioFrequency-0" value="Monthly (once a month)" />

Script: 脚本:

 $("input[type='radio']").change(function () {
            if ($(this).val() == "Monthly (once a month)") {
                $("#ddlMth").show();
            } else {
                $("#ddlMth").hide();
            }
        });

jsfiddle 的jsfiddle

Take out the onclick event, you are mixing javascript and jquery. 取出onclick事件,您正在混合javascript和jquery。 You need something like this: 您需要这样的东西:

<script>
$( function ()
{
    $("input[type='radio']").change(function() {
        if ($(this).val() == "Monthly (once a month)") {
            $("#ddlMth").show();
        }
        else {
            $("#ddlMth").hide();
        }
     });
});
</script>

This will set up a handler that will fire every time that the radio button changes. 这将设置一个处理程序,该处理程序将在单选按钮每次更改时触发。

First of all you are using jQuery for displaying and hiding the div section. 首先,您使用jQuery来显示和隐藏div部分。 In your input element you have added onClick listener, which is the part of javascript. 在输入元素中,您添加了onClick侦听器,它是javascript的一部分。 In your code you have used change method, which is the part of jQuery. 在您的代码中,您使用了change方法,这是jQuery的一部分。 So in effect you are using two listeners which is has no meaning at all. 因此,实际上您正在使用两个完全没有意义的侦听器。

So you can solve this issue in two way. 因此,您可以通过两种方式解决此问题。

1) use onclick event in your input part and make a script for it 1)在输入部分中使用onclick事件并为其编写脚本

2) use click event of jquery(for this, you dont need to provide event listeners in input element) 2)使用jquery的click事件(为此,您不需要在输入元素中提供事件监听器)

note: click and change event has lot of changes in its working. 注意:点击更改事件的工作方式有很多变化。

This is the javascript approach. 这是JavaScript方法。 You can achieve this in lot ways. 您可以通过多种方式实现这一目标。 This is only one method.this will trigger when you click on the radio button. 这只是一种方法。单击单选按钮时将触发此方法。

<input type="radio" name="input_name" onclick="myMyth()" />


<script>
function myMth()
{
    var parentDiv=document.getElementById("ddlMth").parentNode.parentNode;  //gets the div element with class 'control-label'
    var displayStatus=parentDiv.style.display;//gets the status of display element of div
   if(displayStatus=="none")
   {
      //if display="none" make it block. so that dropdown will appear
        parentDiv.style.display="block";
   }
   else
   {
       //if display="block" make it block. so that dropdown will disappear
       parentDiv.style.display="none";
   }
}

second approach is jquery for this, you dont need to specify any listeners in input element 第二种方法是jquery,您无需在输入元素中指定任何侦听器

 <input type="radio" name="input_name" />

Lot of jQuery solutions is available here. 这里有很多jQuery解决方案。 Use any one of those. 使用其中任何一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM