简体   繁体   English

如何使用Javascript或Jquery选中复选框值并显示到输入元素?

[英]How to get checkbox value checked and show to input element using Javascript or Jquery?

but the value is only displayed when I click on the checkbox and it does not display the pre-selected values, I want it to show the selected results even if I do not click the checkbox, please correct this code Help me or give me some example code so I can add, thank you! 但是该值仅在我单击复选框时显示,并且不显示预选的值,即使我不单击该复选框,我也希望它显示选定的结果,请更正此代码。示例代码,我可以添加,谢谢!

<script type="text/javascript">
    function thayTheLoai() {
        var huunhan_theloai = [];
        $.each($("input[type='checkbox']:checked"), function() {
            huunhan_theloai.push($(this).val());
        });
        document.getElementById("input").value = huunhan_theloai.join(", ");
    }
</script>
<label for="default" onclick="thayTheLoai();" class="btn btn-default">
    <input type="checkbox" value="1" id="theloai1" class="badgebox" name="theloai[]" ></input>
</label>
<input id="input" type="text"></input>

<script>
    $('#theloai1').prop('checked', true);
</script>

This is demo : https://anifast.com/includes/test.php , I want to not need to click the checkbox and still display the checked results 这是演示: https : //anifast.com/includes/test.php ,我不需要单击复选框并仍然显示选中的结果

Use this code and don't forget to use jquery library. 使用此代码,不要忘记使用jquery库。

$(document).ready(function(){
    $('#theloai1').prop('checked', true);
    var huunhan_theloai = [];
    $.each($("input[type='checkbox']:checked"), function() {
        huunhan_theloai.push($(this).val());
    });
    document.getElementById("input").value = huunhan_theloai.join(", ");
})


function thayTheLoai() {
    if ($('input[name="theloai[]"]:checked').length > 0) {
        $("#input").val(1);
    }else{
        $("#input").val(0);
    }
}

You have 2 ways of doing it: 您有2种方法可以做到:

  1. you update the input field from your PHP and MySQL to have the checked="checked" on the selected input fields and the run the function on load: 您可以从PHP和MySQL更新输入字段,以在选定的输入字段上checked="checked" ,然后在加载时运行该函数:

JS JS

function thayTheLoai() {
    var huunhan_theloai = [];
    $.each($("input[type='checkbox']:checked"), function() {
        huunhan_theloai.push($(this).val());
    });
    $('#input').val( huunhan_theloai.join(", ") );
}

$(document).ready(function(){
    thayTheLoai();
});

HTML 的HTML

<label for="theloai1" onclick="thayTheLoai();" class="btn btn-default">
    <input type="checkbox" value="1" id="theloai1" class="badgebox" name="theloai[]" />
</label>
<label for="theloai2" onclick="thayTheLoai();" class="btn btn-default">
    <input type="checkbox" value="2" id="theloai2" class="badgebox" name="theloai[]" checked="checked" />
</label>
<input id="input" type="text"/>
  1. if you want to update the input field using the $('#theloai1').prop('checked', true); 如果要使用$('#theloai1').prop('checked', true);更新输入字段$('#theloai1').prop('checked', true); , you should change it so that it also triggers the onchanged event on the input field like this: ,您应该对其进行更改,以使其也触发输入字段上的onchanged事件,如下所示:

$('#theloai1').prop('checked', true).trigger('change');

NOTES 笔记

  1. this code is not tested, but it should work 此代码未经测试,但应该可以工作

  2. make sure you write valid HTML otherwise you might get unexpected results (I fixed your HTML as well) 确保您编写有效的HTML,否则可能会得到意外的结果(我也修复了HTML)

<label for="default" class="btn btn-default">
<input type="checkbox" value="1" id="theloai1" class="badgebox" name="theloai[]" ></input>
<input type="checkbox" value="2" id="theloai2" class="badgebox" name="theloai[]" ></input>
<input type="checkbox" value="3" id="theloai3" class="badgebox" name="theloai[]" ></input>
<input type="checkbox" value="4" id="theloai4" class="badgebox" name="theloai[]" ></input>

</label>
<input id="input" type="text"></input>



$(document).ready(function(){
    $('#theloai1').prop('checked', true);
    var huunhan_theloai = [];
    $.each($("input[type='checkbox']:checked"), function() {
        huunhan_theloai.push($(this).val());
    });
    document.getElementById("input").value = huunhan_theloai.join(", ");
})

$("input[type='checkbox']").on("change", function(){
    checked_id = $(this).attr("id");
    if ($(this).prop("checked") == true) {
        $("input[type='checkbox']").each(function(){
            if ($(this).attr("id") != checked_id) {
                $(this).prop("checked", false);
            }
        })
        $("#input").val($(this).val());
    }else{
        $("#input").val('some default value');
    }
})

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

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