javascript – 使用jQuery复选框选择的MultiSelect下拉列表

我正在尝试使用jQuery手动创建一个带有多选复选框的下拉列表.

我不想使用除jQuery之外的任何内置库.我尝试实现功能,但问题是,当我选中复选框时,数据将弹出输入字段,如果我第二次点击,我可以看到复选框未选中,数据不会附加到上一个.

请告诉我代码中哪里出错了.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Untitled Document</title>
<style>
    .multiselect {
        width: 200px;
    }
    .selectBox {
        position: relative;
    }
    .selectBox select {
        width: 100%;
        font-weight: bold;
    }
    .overSelect {
        position: absolute;
        left: 0; right: 0; top: 0; bottom: 0;
    }
    #presentationTable {
        display: none;
        border: 1px #dadada solid;
    }
    #presentationTable label {
        display: block;
    }
    #presentationTable label:hover {
        background-color: #1e90ff;
    }
</style>

</head>

<body>
<form>
    <div class="multiselect">
        <div class="selectBox" onClick="showCheckboxes()">
            <input type="text" id="presentatioonTableimputValue"><img src="http://siged.sep.gob.mx/analytics/res/s_blafp/uicomponents/obips.CommonIconSets/dropdownfilled_en_choicelistmulti.png" />
            <div class="overSelect"></div>
        </div>
        <div id="presentationTable"> </div>
    </div>
</form>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script>
    var expanded = false;
    function showCheckboxes()
    {
        var abc = '<label><input type="checkbox" id="one" value="1"/>First1 checkbox</label> <label><input type="checkbox" id="two" value="two"/>Second checkbox </label> <label><input type="checkbox" id="three" value="three"/>Third checkbox</label>';
        $('#presentationTable').html(abc);
                if (!expanded) {
                    $('#presentationTable').show();
                    expanded = true;
                } else {
                    $('#presentationTable').hide();           
                    expanded = false;
                }       
    } 


    function updateTextArea() 
    {
        var allVals = [];
        $('#presentationTable :checked').each(function () {
            allVals.push($(this).val());
        });
        $('#presentatioonTableimputValue').val(allVals);
    }

    $(document).click(function(event){
        var $trigger = $(".multiselect");
        if($trigger !== event.target && !$trigger.has(event.target).length){
            $("#presentationTable").slideUp("fast");

             updateTextArea();
        }           
    });           
</script>
</body>
</html>

解决方法:

试试这个:

JS

var expanded = false;
populateCheckbox();
function populateCheckbox(){
    var abc = '<label><input type="checkbox" id="one" value="1"/>First1   checkbox</label> <label><input type="checkbox" id="two" value="two"/>Second checkbox </label> <label><input type="checkbox" id="three" value="three"/>Third checkbox</label>';
    $('#presentationTable').html(abc);
}
function showCheckboxes()
 {

            if (!expanded) {
                $('#presentationTable').show();
                expanded = true;
            } else {
                $('#presentationTable').hide();           
                expanded = false;
            }       
} 


function updateTextArea() 
{
        var allVals = [];
        $('#presentationTable :checked').each(function () {
           allVals.push($(this).val());
        });
        $('#presentatioonTableimputValue').val(allVals);

    }
$("#presentationTable label input").click(function(){
    updateTextArea();
});
 $(document).click(function(event){
    var $trigger = $(".multiselect");
    if($trigger !== event.target && !$trigger.has(event.target).length){
        $("#presentationTable").slideUp("fast");

        // updateTextArea();
    }           
});   

每次单击输入字段时,都会创建新的复选框,这就是问题所在.
希望它能解决你的问题.

相关文章

1.第一步 设置响应头 header(&#39;Access-Control-Allow...
$.inArray()方法介绍 $.inArray()函数用于在数组中搜索指定的...
jquery.serializejson.min.js的妙用 关于这个jquery.seriali...
JS 将form表单数据快速转化为object对象(json对象) jaymou...
jQuery插件之jquery.spinner数字智能增减插件 参考地址:http...