我有一个具有相同类的复选框,基本上它的作用是,如果我选中该复选框,它将检查与该复选框相同的类,并返回值。但是它不会直接返回,我需要单击某个地方,然后将其注册到文本框。当我选中复选框时,我需要的是直接输入文本框的值。该表代码来自php,因此class + number将会更改,并且无法修复。
下面是代码段。
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" />
答案 0 :(得分:2)
尝试使用$("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" />
答案 1 :(得分:1)
很难从问题中确切地找到您想要的东西,但是我已经尝试了。
首先,您的HTML是否存在错误?应该在下面:
<td><input type="checkbox" class="something2" name="student_name[]" value='' /></td>
<td><input type="checkbox" class="something2" name="user_id[]" value='2' /></td>
实际上是:
<td><input type="checkbox" class="something2" name="student_name[]" value='2' /></td>
<td><input type="checkbox" class="something2" name="user_id[]" value='' /></td>
请注意这些值会交换。
我认为要解决您的问题,您可以像下面这样在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" />
我也尝试过改善您的格式设置,并坚持只使用jquery选择器
答案 2 :(得分:1)
我为你招来了她的工作。 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" />
功能...
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);
})