如何在文本框

时间:2018-03-20 09:50:10

标签: javascript arrays codeigniter-2

我有三个文本框作为数组给出,我想添加两个文本框并在第三个文本框中获得结果,如下所示。

<tbody>
   <?php 
     $i=1;
     foreach ($sub as $row) 
     {
   ?>
<tr>
<td><?php echo $i++;?>
</td>
<td>  <div><span style="border: none;"><input type="text" name="subject[]" class="textbox" value="<?php echo $row->subject_name;?>"></span></div></td>
<td><input type="text" name="mark-1[]" value="" id="mark1"></td>
<td><input type="text" name="mark-2[]" value="" id="mark2"></td>
<td><input type="text" name="total-mark[]" value="" id="totmark"  onclick="add_number()"></td>
</tr>
<?php  }  ?>
<tr>
<th colspan="4">Total Mark</th>
<td></td></tr>
</tbody>

我使用了以下脚本及其运行但在第一个循环中获得结果

 <script type="text/javascript">
function add_number() {
var first_number = parseInt(document.getElementById("mark1").value);
var second_number = parseInt(document.getElementById("mark2").value);
var result = first_number + second_number;
document.getElementById("totmark").value = result;
 }
</script>

请帮助我们得到答案

2 个答案:

答案 0 :(得分:0)

问题是每次迭代的重复ID,mark1,mar2,mark1,mark2等。函数getElementById返回第一个元素。

另一种方法是使用函数closest('tr'),然后按名称[name="mark-1[]"][name="mark-2[]"]查找。然后使用此.querySelector('[name="total-mark[]"]')找到要存储结果的字段。

最后,使用函数addEventListener,您可以将事件点击绑定到整个元素集[name="total-mark[]"]

&#13;
&#13;
document.querySelectorAll('[name="total-mark[]"]').forEach(function(elem) {
  elem.addEventListener('click', add_number);
});

function add_number() {
  var tr = this.closest('tr');
  
  var first_number = tr.querySelector('[name="mark-1[]"]').value;
  var second_number = tr.querySelector('[name="mark-2[]"]').value;
  var result = Number(first_number) + Number(second_number);
  
  this.value = result;
}
&#13;
<table>
  <tbody>
    <tr>
      <td><input type="text" name="mark-1[]" value="" ></td>
      <td><input type="text" name="mark-2[]" value="" ></td>
      <td><input type="text" name="total-mark[]" value=""  placeholder='click to totalize'></td>
    </tr>
    <tr>
      <td><input type="text" name="mark-1[]" value="" ></td>
      <td><input type="text" name="mark-2[]" value="" ></td>
      <td><input type="text" name="total-mark[]" value=""  placeholder='click to totalize'></td>
    </tr>
  </tbody>
</table>
&#13;
&#13;
&#13;

建议使用PHP变量$i中的当前索引,并为每个字段添加特定ID,这样您就可以直接获取元素。例如,这两个字段将包含以下ID:mark-1-1[]mark-1-2[]mark-2-1[]mark-2-2[],依此类推。

此方法使用属性dataset

&#13;
&#13;
document.querySelectorAll('[name="total-mark[]"]').forEach(function(elem) {
  elem.addEventListener('click', add_number);
});

function add_number() {
  var field = this.dataset.field;
  
  var first_number = document.getElementById('mark-1-' + field + '[]').value;
  var second_number = document.getElementById('mark-2-' + field + '[]').value;
  
  var result = Number(first_number) + Number(second_number);
  this.value = result;
}
&#13;
<table>
  <tbody>
    <tr>
      <td><input type="text" id="mark-1-1[]" value="" ></td>
      <td><input type="text" id="mark-2-1[]" value="" ></td>
      <td><input type="text" data-field='1' name="total-mark[]" value=""  placeholder='click to totalize'></td>
    </tr>
    <tr>
      <td><input type="text" id="mark-1-2[]" value="" ></td>
      <td><input type="text" id="mark-2-2[]" value="" ></td>
      <td><input type="text" data-field='2' name="total-mark[]" value=""  placeholder='click to totalize'></td>
    </tr>
  </tbody>
</table>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

HTML

<tbody>
   <?php 
     $i=1;
$k=1;
     foreach ($sub as $row) 
     { $k++;
   ?>
<tr>
<td><?php echo $i++;?>
</td>
<td>  <div><span style="border: none;"><input type="text" name="subject[]" class="textbox" value="<?php echo $row->subject_name;?>"></span></div></td>
<td><input type="text" name="mark-1[]" value="" id="mark1-<?php echo $k;?>"></td>
<td><input type="text" name="mark-2[]" value="" id="mark2-<?php echo $k;?>"></td>
<td><input type="text" name="total-mark[]" value="" id="totmark-<?php echo $k;?>"  onclick="add_number(<?php echo $k;?>)"></td>
</tr>
<?php  }  ?>
<tr>
<th colspan="4">Total Mark</th>
<td></td></tr>
</tbody>

脚本

   <script type="text/javascript">
function add_number(k) {
var first_number = parseInt(document.getElementById("mark1-"+k).value);
var second_number = parseInt(document.getElementById("mark2-"+k).value);
var result = first_number + second_number;
document.getElementById("totmark-"+k).value = result;
 }
</script>