我有两个文件,假设1.php和2.php
1.php是我的提取数据,因此其中包含
echo '<tr onclick="javascript:showRow(this);">';
echo "<td><input type=\"checkbox\" name=\"user_id[]\" value='".$user_id."'/>$user_id</td>";
我的第二个php 2.php包含
<script type="text/javascript">
function showRow(row)
{
var x=row.cells;
document.getElementById("custID").value = x[3].innerHTML;
}
</script>
和
<input class="form-control" name="event_name" id="custID" type="text" maxlength="255" />
我的问题是从复选框到文本框仅显示1个值。我希望它显示来自多个复选框的多个值,以便我可以将多个值发送到表单发布。在此先感谢陌生人
答案 0 :(得分:2)
这可能就是您想要的。只需使用addValue而不是showRow函数
function addValue(input){
//select all checkboxes with name userid that are checked
var checkboxes = document.querySelectorAll("input[name='user_id[]']: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("custID").value = values;
}
<table>
<!-- Change the onclick event handler from tr to checkbox -->
<tr >
<td><input type="checkbox" onclick="addValue(this)" name="user_id[]" value='1'/>1</td>
</tr>
<tr >
<td><input type="checkbox" onclick="addValue(this)" name="user_id[]" value='2'/>2</td>
</tr>
<tr >
<td><input type="checkbox" onclick="addValue(this)" name="user_id[]" value='3'/>3</td>
</tr>
</table>
<input class="form-control" name="event_name" id="custID" type="text" maxlength="255" />