[英]How to Checked a checkbox when there is a certain value in the text field
I have a textfield which contains certain value: 我有一个包含某些值的文本字段:
<input class="form-control" name="user_id" id="studID" type="text" maxlength="255" value="61,62,63,64"/>
this is the input checkbox 这是输入复选框
<input type="checkbox" name="user_id[] value="61" />
<input type="checkbox" name="user_id[] value="62" />
<input type="checkbox" name="user_id[] value="63" />
*note i run this checkbox in php thats why it contains the [] for array. *请注意,我在php中运行此复选框,这就是为什么它包含数组的[]。
I want the checkbox to checked if there is a value of 61 inside the textfield. 我希望复选框检查文本字段内是否有值61。 I want it to be dynamic because it is from database, because value can be change and it is not fixed.
我希望它是动态的,因为它来自数据库,因为值可以更改并且不是固定的。 How do i do this?
我该怎么做呢?
thanks in advance 提前致谢
You have to iterate over the inputs and then check the condition: 您必须遍历输入,然后检查条件:
document.querySelectorAll("input").forEach(function(i) { if (i.value.indexOf("61") > -1) { i.checked = true; } });
<input type="checkbox" name="user_id[]" value=" 61 " /> <input type="checkbox" name="user_id[]" value="62" /> <input type="checkbox" name="user_id[]" value=" 63" />
You can try something like this 您可以尝试这样的事情
let ids = $('#studID').val().split(','); ids.forEach(function(id) { $('input[type="checkbox"][value="'+id+'"]').prop('checked',true); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input class="form-control" name="user_id" id="studID" type="text" maxlength="255" value="61,62,63,64"/> <input type="checkbox" name="user_id[]" value="61" /> <input type="checkbox" name="user_id[]" value="62" /> <input type="checkbox" name="user_id[]" value="63" /> <input type="checkbox" name="user_id[]" value="66" /> <input type="checkbox" name="user_id[]" value="67" /> <input type="checkbox" name="user_id[]" value="68" />
let ids = $('#studID').val().split(','); ids.forEach(function(id) { $('input[type="checkbox"][value="'+id+'"]').prop('checked',true); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input class="form-control" name="user_id" id="studID" type="text" maxlength="255" value="61,62,63,64"/> <input type="checkbox" name="user_id[]" value="61" /> <input type="checkbox" name="user_id[]" value="62" /> <input type="checkbox" name="user_id[]" value="63" /> <input type="checkbox" name="user_id[]" value="66" /> <input type="checkbox" name="user_id[]" value="67" /> <input type="checkbox" name="user_id[]" value="68" />
plain javascript 普通的javascript
let ids = document.querySelector("#studID").value.split(','); ids.forEach(function(id) { var checkbox = document.querySelector("input[name='user_id[]'][value='"+id+"']"); if(checkbox) { checkbox.checked = true; } })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input class="form-control" name="user_id" id="studID" type="text" maxlength="255" value="61,62,63,64"/> <input type="checkbox" name="user_id[]" value="61" /> <input type="checkbox" name="user_id[]" value="62" /> <input type="checkbox" name="user_id[]" value="63" /> <input type="checkbox" name="user_id[]" value="66" /> <input type="checkbox" name="user_id[]" value="67" /> <input type="checkbox" name="user_id[]" value="68" />
Add a change
listener to the text field. 将
change
侦听器添加到文本字段。 It should loop through the checkboxes, either checking or unchecking them depending on whether their values match the input. 它应该遍历复选框,根据其值是否与输入相匹配来选中或取消选中它们。
document.querySelector("#studID").addEventListener("change", function() { var values = this.value.split(","); var checkboxes = document.querySelectorAll(`input[name='user_id[]']`); checkboxes.forEach(cb => cb.checked = values.includes(cb.value)); });
<input class="form-control" name="user_id" id="studID" type="text" maxlength="255" value="61,62,63,64" /> <br> <input type="checkbox" name="user_id[]" value="61" /> <input type="checkbox" name="user_id[]" value="62" /> <input type="checkbox" name="user_id[]" value="63" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.