简体   繁体   English

当文本字段中有特定值时如何检查复选框

[英]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.

相关问题 取消选中复选框后如何清除jquery.businesshours中文本字段的值? - How to clear value of text field in jquery.businesshours when checkbox is de-checked? 选中复选框时,文本字段如何显示存储在“paramsText”中的值? - How could the text field show the value stored in the `paramsText` when the checkbox is checked? 选中具有特定值的复选框时如何显示元素? - How to display an element when the checkbox with certain value is checked? 选中复选框时如何复制输入字段值? - How to copy input field value when checkbox is checked? 选中复选框时如何使字段为必填项 - how to make a field requried when the checkbox is checked 选中复选框时如何更改文本显示 - How to change text display when checkbox checked 选中复选框时如何自动填充文本区域 - How to autofill a text area when a checkbox is checked 当用户输入某个值时,如何将复选框标记为已选中? - How can I mark checkbox checked when the user input a certain value? 如何根据是否选中复选框来更改输入文本字段的值? - How do I change the value of an input text field based on if a checkbox is checked or not? 如何在选中复选框时将 AJAX 值发送为 1,在未选中时发送 0? - How to send AJAX value as 1 when checkbox is checked and 0 when it is not checked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM