javascript - 使用 jQuery 进行复选框选择的 MultiSelect 下拉菜单

标签 javascript jquery html checkbox

我正在尝试使用 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();
    }           
});   

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

关于javascript - 使用 jQuery 进行复选框选择的 MultiSelect 下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37695492/

相关文章:

javascript - 什么是正则表达式匹配标题 : [name]?

javascript - 使用 HTML/CSS/Javascript 在新页面中打开链接

javascript - jQuery - HTML 表到数据表

html - 没有calc如何实现两列布局?

javascript - 在表单提交之前调用函数的更好方法是什么?

javascript - 防止注入(inject)的标签受到父 CSS 的影响

javascript - 从sql server数据库自动填充jsp中文本字段的值

jquery - Bootstrap : Proper way of linking JavaScript files in HTML layout

javascript - 使用 jquery resizable 调整 DIV 的句柄大小

jquery - typeerror jquery(...).isotope 不是函数