Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在选择单选按钮上显示divjquery_Javascript_Jquery - Fatal编程技术网

Javascript 在选择单选按钮上显示divjquery

Javascript 在选择单选按钮上显示divjquery,javascript,jquery,Javascript,Jquery,我使用三个单选按钮来显示三个不同的div。但它不起作用。 这是我的剧本 <script> $(document).ready(function() { $("input[name$='Want_To']").click(function() { var test = $(this).val(); $("div.desc").hide(); $("#Cars" + test).show(); }); }); </script> $(文档).r

我使用三个单选按钮来显示三个不同的div。但它不起作用。 这是我的剧本

<script>

$(document).ready(function() {
$("input[name$='Want_To']").click(function() {
    var test = $(this).val();

    $("div.desc").hide();
    $("#Cars" + test).show();
});
});
</script>

$(文档).ready(函数(){
$(“输入[name$='Want_To'])。单击(函数(){
var test=$(this.val();
$(“div.desc”).hide();
$(“#汽车”+测试).show();
});
});
以下是我的js小提琴作品演示:

你的js呢

$(document).ready(function() {
    $("input[name$='Want_To']").click(function() {
      $('#sell,#rent,#PG').hide();
        var test = $(this).val();

        $("div.desc").hide();
        $("#" + test).show();
    });
});
我在这里使用了一点不同的方法


Chec here demo

首先在yopur页面上添加jquery

那么HTML将是

<div class="usertype">
    <ul>
        <li id="userpr"><label>I want to:</label>
        <input type="radio" name="Want_To" value="sell"  checked /><label class="new-label selected" for="Sell">Sell</label>    
                <input type="radio" name="Want_To" value="rent"  /><label class="new-label" for="Rent/Lease">Rent/Lease</label>
                <input type="radio" name="Want_To" value="PG"  /><label class="new-label" for="Want-To-PG">PG</label> </li>
        <div id="sell" class="desc">
            Show sell div
        </div>
        <div id="rent" class="desc">
            Show rent div
        </div>
        <div id="PG" class="desc">
            Show PG div
        </div>
    </ul>
</div>
$(document).ready(function(){
       $("input[type='radio']").change(function() {
            var test = $(this).val();
            $("div.desc").hide();
            $("#" + test).show();
        });
});
试试这个 HTML


更新脚本并添加Jquery

$(document).ready(function() {
    $("input[name$='Want_To']").click(function() {
        var test = $(this).val();

        $("div.desc").hide();
        $("#" + test).show();
    });
});
和更新的html

<div class="usertype">
    <ul>
        <li id="userpr"><label>I want to:</label>
        <input type="radio" name="Want_To" value="sell" id="Sell"  checked /><label class="new-label selected" for="Sell">Sell</label>  
                <input type="radio" name="Want_To" value="rent"  id="Rent/Lease" /><label class="new-label" for="Rent/Lease">Rent/Lease</label>
                <input type="radio" name="Want_To" value="PG" id="Want-To-PG" /><label class="new-label" for="Want-To-PG">PG</label> </li>
        <div id="sell" class="desc" style="display:none">
            Show sell div
        </div>
        <div id="rent" class="desc" style="display:none">
            Show rent div
        </div>
        <div id="PG"  class="desc" style="display:none">
            Show PG div
        </div>
    </ul>
</div>

  • 我想: 卖 租金/租赁 PG
  • 展销部 展览租金部 表演节目组

我希望这对你有用祝你好运

您需要根据您的div更改无线电元素的值, 还要为所有div容器创建一个公共类,以便一次只显示一个div

试试看,我不喜欢

$(document).ready(function () {  
    $(".item-div").hide();
    // show checked radio div
    $('#'+$('[name="Want_To"]:checked').val()).show();
    $("input[name='Want_To']").click(function () {
        var test = $(this).val();
        $(".item-div").hide();
        $("div.desc").hide();
        $("#" + test).show();
    });
});


浏览器控制台中有任何错误请检查my jsfiddle@user3575181添加jquery,您的代码工作需要做一些更改。。但当选择发生变化时,您似乎忘记了隐藏另一个选择……是的,我知道@GujjuDeveloper,但满足了要求;)现在应该是@GujjuDeveloper了
$(document).ready(function() {
    $("input[name$='Want_To']").click(function() {
        var test = $(this).val();

        $("div.desc").hide();
        $("#" + test).show();
    });
});
<div class="usertype">
    <ul>
        <li id="userpr"><label>I want to:</label>
        <input type="radio" name="Want_To" value="sell" id="Sell"  checked /><label class="new-label selected" for="Sell">Sell</label>  
                <input type="radio" name="Want_To" value="rent"  id="Rent/Lease" /><label class="new-label" for="Rent/Lease">Rent/Lease</label>
                <input type="radio" name="Want_To" value="PG" id="Want-To-PG" /><label class="new-label" for="Want-To-PG">PG</label> </li>
        <div id="sell" class="desc" style="display:none">
            Show sell div
        </div>
        <div id="rent" class="desc" style="display:none">
            Show rent div
        </div>
        <div id="PG"  class="desc" style="display:none">
            Show PG div
        </div>
    </ul>
</div>
$(document).ready(function () {  
    $(".item-div").hide();
    // show checked radio div
    $('#'+$('[name="Want_To"]:checked').val()).show();
    $("input[name='Want_To']").click(function () {
        var test = $(this).val();
        $(".item-div").hide();
        $("div.desc").hide();
        $("#" + test).show();
    });
});
$(document).ready(function() {
    $("input[name$='Want_To']").click(function() {
        var test = $(this).val();
        console.log("#"+test);
        $(".a").hide();
        $("#"+test).show();
    });
});