[英]How to display multiple checkbox value to textbox from one file to another
I have two file lets assume 1.php and 2.php我有两个文件让我们假设 1.php 和 2.php
1.php is my fetch data so it contains 1.php 是我获取的数据,所以它包含
echo '<tr onclick="javascript:showRow(this);">';
echo "<td><input type=\"checkbox\" name=\"user_id[]\" value='".$user_id."'/>$user_id</td>";
my second php 2.php contains我的第二个 php 2.php 包含
<script type="text/javascript">
function showRow(row)
{
var x=row.cells;
document.getElementById("custID").value = x[3].innerHTML;
}
</script>
and和
<input class="form-control" name="event_name" id="custID" type="text" maxlength="255" />
My problem is that it only displays 1 value from the checkbox to the textbox.我的问题是它只显示从复选框到文本框的 1 个值。 I would like it to display multiple value from the multiple checkbox so i could send multiple value to a form post.
我希望它显示多个复选框中的多个值,以便我可以将多个值发送到表单帖子。 Thanks in advance fellow stranger
提前谢谢陌生人
may be this is what you want .可能这就是你想要的。 just use addValue instead of your showRow function
只需使用 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" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.