使用单选按钮和JQuery显示/隐藏Dropbox

时间:2015-04-15 17:44:32

标签: javascript jquery html radio-button show-hide

这是我的HTML:

<input type="radio" value="rural" id="rural" name="landtype" checked>Rural
<input type="radio" value="urban" id="urban" name="landtype" checked>Urban

<select id="budget1" class="dbox1">
                    <option value="9000">8000php-10000php</option>
                    <option value="13000">12000php-14000php</option>
                    <option value="17000">16000php-18000php</option>
                </select>

<select id="budget2" class="dbox2">
                    <option value="10000">9000php-11000php</option>
                    <option value="14000">12000php-15000php</option>
                    <option value="18000">17000php-19000php</option>
                </select>

这是我的javascript代码:

<script type="text/javascript">

    $(document).ready(function(){
                      $('input[type="radio"]').click(function(){
                           if($(this).attr("value")=="rural"){

                                document.getElementById("budget2").hide();
                                document.getElementById("budget1").show();

                           }
                           if($(this).attr("value")=="urban"){
                                document.getElementById("budget1").hide();
                                document.getElementById("budget2").show();
                           }
    });
    });

</script>

我想要的是,如果我点击我的&#34; Rural&#34;单选按钮,&#34; dbox2&#34;然后会隐藏&#34; dbox1&#34;将会呈现。 如果我点击我的&#34; Urban&#34;单选按钮,&#34; dbox1&#34;然后将隐藏&#34; dbox2&#34;将显示。

到目前为止我得到的是一个&#34;错误&#34;来自&#34; document.getElementById("budget1").hide();&#34;说&#34; Uncaught TypeError:undefined不是函数&#34;

任何解决方案?

3 个答案:

答案 0 :(得分:1)

如果您正在使用jQuery,请使用它并更改这些行:

document.getElementById("budget2").hide();
document.getElementById("budget1").show();

为:

$("#budget2").hide();
$("#budget1").show();

编辑:抱歉,我忘了添加ID选择器。

答案 1 :(得分:1)

hide()和show()是jQuery函数,这就是你获取这些错误的原因。使用$()来获取这些元素,然后调用hide和show。

答案 2 :(得分:0)

$("#budget2").hide();


$("#budget1").show();