如何显示具有选定值和说明的复选框



当复选框被选中时,它将被选中的值显示为文本如何使它显示一个复选框,旁边出现文本和描述。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
    <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css">
    <script type="text/javascript">
        $(document).ready(function () {
            $(document).on("click", ":checkbox", function (e) {
                if ($(this).is(':checked')) {
                    var checkbox =  $("#sample").prepend('<div id="anotherdiv">' + $(this).attr('value') + '</div>');
                    $("#sample").prepend(checkbox); 
                }
                else {
                    $('#anotherdiv').remove();
                }
            });
        });
    </script>
</head>
<body class="center" style= "margin-top: 150px;">
    <div id="sample"></div>
    <input type="checkbox" name="vehicle" value="Bike">I have a bike<br>
</body>
</html>

我不知道我是否正确理解你的问题,但如果你想value属性的值出现在复选框代码之前或之后,将是:
html:

之前
<script type="text/javascript">
$(document).ready(function(){
     $(document).on("click", "input[type='checkbox']", function (e) {   
        var id=$(this).attr('id')
        if($('#' + id).is(":checked")){
             $( '<span class="anotherdiv">' + $(this).attr('value') + '</span>').appendTo('body').insertBefore($(this)) 
            }else{
              $(this).prev().remove();  
            }
        })
    })
</script>
:后

<script type="text/javascript">
    $(document).ready(function(){
        $(document).on("click", "input[type='checkbox']", function (e) {
            var id=$(this).attr('id')
            if($('#' + id).is(":checked")){
                 $( '<span class="anotherdiv">' + $(this).attr('value') + '</span>').appendTo('body').insertAfter($(this)) 
                }else{
                  $(this).next().remove();  
                }
            })
        })
    </script>
html:

 <input id="a"type="checkbox" name="vehicle" value="Bike"><br>
 <input id="b"type="checkbox" name="car" value="Car"><br>

如果是由于我的英语水平太差,没有正确理解你的问题,我很抱歉耽误了你的时间。

最新更新