简体   繁体   English

当我选中一个复选框(相同的类)并将值插入文本框中时,如何检查另一个复选框

[英]How do i checked another checkbox when i checked a checkbox(same class) and insert value into a textbox

i have a checkbox with same class, basically what it does is that if i checked the checkbox it will checked the same class as the checkbox and it will return value. 我有一个具有相同类的复选框,基本上它的作用是,如果我选中该复选框,它将检查与该复选框相同的类,并且它将返回值。 But it will not return directly, i need to click somewhere then it register to the textbox. 但是它不会直接返回,我需要单击某个地方,然后将其注册到文本框。 What i need is straight-forward value into the textbox when i checked the checkbox. 当我选中复选框时,我需要的是直接输入文本框的值。 The table code is from php so the class+number will change and its not fix. 该表代码来自php,因此class + number将会更改,并且无法修复。

Below is the snippet for it. 以下是其片段。

 function doSomethingElse(row) { //select all checkboxes with name userid that are checked var checkboxes = document.querySelectorAll("input[name='student_name[]']:checked") var values = ""; //append values of each checkbox into a variable (seperated by commas) for (var i = 0; i < checkboxes.length; i++) { values += checkboxes[i] .value + "," } //remove last comma values = values.slice(0, values.length - 1) //set the value of input box document.getElementById("stud_name").value = values; } $("input:checkbox").change(function() { //alert('Ok!'); var value = $(this).attr("class"); $(":checkbox[class='" + value + "']").prop("checked", this.checked); }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr onclick="doSomethingElse(this);"> <td><input type="checkbox" class="something1" name="user_id[]" value='' /></td> <td><input type="checkbox" class="something1" name="student_name[]" value='1' /></td> <td><input type="checkbox" class="something2" name="student_name[]" value='' /></td> <td><input type="checkbox" class="something2" name="user_id[]" value='2' /></td> </tr> </table> <input class="form-control" name="stud_name" id="stud_name" type="text" maxlength="255" /> 

Try using $("input:checkbox").click(function () { instead of change 尝试使用$("input:checkbox").click(function () {代替change

 function doSomethingElse(row){ //select all checkboxes with name userid that are checked var checkboxes = document.querySelectorAll("input[name='student_name[]']:checked") var values = ""; //append values of each checkbox into a variable (seperated by commas) for(var i=0;i<checkboxes.length;i++){ values += checkboxes[i].value + "," } //remove last comma values = values.slice(0,values.length-1); //set the value of input box document.getElementById("stud_name").value = values; } $("input:checkbox").click(function () { //alert('Ok!'); var value = $(this).attr("class"); $(":checkbox[class='" + value + "']").prop("checked", this.checked); }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr onclick="doSomethingElse(this);"> <td><input type="checkbox" class ="something1"name="user_id[]" value='ID1' /></td> <td><input type="checkbox" class ="something1"name="student_name[]" value='NAME1' /></td> <td><input type="checkbox" class ="something2"name="student_name[]" value='NAME2' /></td> <td><input type="checkbox" class ="something2"name="user_id[]" value='ID2' /></td> </tr> </table> <input class="form-control" name="stud_name" id="stud_name" type="text" maxlength="255" /> 

It's hard to figure out exactly what you want from your question, but I've given it a try. 很难从问题中确切地找出您想要什么,但是我已经尝试了一下。

Firstly, is there an error in your HTML? 首先,您的HTML是否有错误? Should the below: 应该是以下情况:

    <td><input type="checkbox" class="something2" name="student_name[]" value='' /></td>
    <td><input type="checkbox" class="something2" name="user_id[]" value='2' /></td>

actually be: 实际上是:

    <td><input type="checkbox" class="something2" name="student_name[]" value='2' /></td>
    <td><input type="checkbox" class="something2" name="user_id[]" value='' /></td>

Notice the values swapped around. 注意这些值交换了。

I think to solve your problem you can do all the work in the onchange like below: 我认为要解决您的问题,您可以像下面这样在onchange中完成所有工作:

 $("input:checkbox").change(function () { //alert('Ok!'); var value = $(this).attr("class"); $(":checkbox[class='" + value + "']").prop("checked", this.checked); var checkboxes = $("input[name='student_name[]']:checked"); var values = ""; //append values of each checkbox into a variable (seperated by commas) for(var i=0;i<checkboxes.length;i++) { values += checkboxes[i].value + "," } //remove last comma values = values.slice(0,values.length-1) //set the value of input box $("#stud_name").val(values); }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr> <td><input type="checkbox" class ="something1"name="user_id[]" value='' /></td> <td><input type="checkbox" class ="something1"name="student_name[]" value='1' /></td> <td><input type="checkbox" class ="something2"name="student_name[]" value='2' /></td> <td><input type="checkbox" class ="something2"name="user_id[]" value='' /></td> </tr> </table> <input class="form-control" name="stud_name" id="stud_name" type="text" maxlength="255" /> 

I've also tried to improve your formatting a bit and stuck to using only jquery selectors 我也尝试过改善您的格式,并坚持只使用jquery选择器

I've got you a working fiddle over her; 我让你为她作工; https://jsfiddle.net/nd01p86t/15/ https://jsfiddle.net/nd01p86t/15/

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
  <tr>
    <td><input type="checkbox" class="something1" name="user_id[]" value='' /></td>
    <td><input type="checkbox" class="something1" name="student_name[]" value='1' /> </td>
    <td><input type="checkbox" class="something2" name="student_name[]" value='' /> </td>
    <td><input type="checkbox" class="something2" name="user_id[]" value='2' /></td>
  </tr>
</table>

<input class="form-control" name="stud_name" id="stud_name" type="text" maxlength="255" />

THE functions... 功能...

function doSomethingElse(row) {
  //select all checkboxes with name userid that are checked
  var checkboxes = document.querySelectorAll("input[name$='[]']:checked")
  // OR...
  //var checkboxes = document.querySelectorAll("input[type='checkbox']:checked")

  //append values of each checkbox into a variable (seperated by commas)

  var values = Array.from(checkboxes).filter( checkbox => checkbox.value ).map( checkbox => checkbox.value ).join(',');

  //set the value of input box
  document.getElementById("stud_name").value = values;
}

//  Attach to the table and delegate 'click' per row
$("table").delegate("tr", "click", doSomethingElse);


$("input:checkbox").click(function() {
  //alert('Ok!');
  var value = $(this).attr("class");
  $(":checkbox[class='" + value + "']").prop("checked", this.checked);
})

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

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