简体   繁体   English

启用“选择”单选按钮值,以div显示并隐藏

[英]On Select radio button value show in div and hide

Here I use 6 radio button for six different value, I want to select one button from in these and the value of select button show in upper div Here I use a java script for show and hide radio button 在这里,我使用6个单选按钮表示六个不同的值,我想从中选择一个按钮,并且select button的值显示在上div中在这里,我使用Java脚本来显示和隐藏单选按钮

<script> $(document).ready(function(){  
   $("#cityshow").click(function(){    
      $("#citybox").toggle();  
   });
});
</script>

Here is My JSfiddle. 这是我的JSfiddle。

Like this? 像这样?

$(document).ready(function(){
    $("#cityshow").click(function(){
        $("#citybox").toggle();
    });
    $("#citybox input:radio").click(function() {
        $("#citybox").toggle();
        $("#cityshow").text($(this).val());
    });
});

Is this what you're after? 这是你所追求的吗?

$(document).ready(function(){
    $("#cityshow").click(function(){
      $("#citybox").toggle();
    });

    $('#citybox input[type="radio"]').click(function(){
        var buttonValue = $(this).val();
        $("#citybox").toggle();
        $('#cityshow').text(buttonValue);
    });
});

JSFiddle 的jsfiddle

UPDATE: 更新:

To insert the same value into the element 将相同的值插入元素

<input id="cityid" type="hidden" name="" value="" >

$(document).ready(function(){
    $("#cityshow").click(function(){
      $("#citybox").toggle();
    });

    $('#citybox input[type="radio"]').click(function(){
        var buttonValue = $(this).val();
        $("#citybox").toggle();
        $('#cityshow').text(buttonValue);
        $('#cityid').val(buttonValue); // inserts value into input
    });
});

Please note, if you intend to use the value of the hidden input in a post request, then you should use the name attribute: 请注意,如果您打算在发布请求中使用隐藏输入的值,则应使用name属性:

<input id="cityid" type="hidden" name="cityid" value="" >

All you need is another handler click() on radio buttons and if they are checked you have to set it in the upper div like following 您需要做的是单选按钮上的另一个处理程序click() ,如果选中它们,则必须在上div中进行设置,如下所示

$('input[type="radio"]').on('click',function(){
     if(this.checked){
        // $(this).closest(".citycollection").siblings(".citybox").text(this.value); // Set value to citybox
         $("#cityshow").text(this.value);
         $("#citybox").hide(); // hide city box
     } 
});

Demo 演示

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

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