如何让onkeyup在下一行工作?

时间:2017-12-14 03:39:35

标签: javascript php codeigniter

案例: 我创建了一个表单输入,显示每个班级的所有学生并逐个输入他们的值。我成功创建onkeyup来计算(N.Akhir)以获得欲望值。但这仅适用于第一行,它不适用于下一行。 enter image description here 如何使onkeyup在下一行工作?

我正在使用codeigniter和javascript来制作此

请参阅下面的代码:

function hitung2() {
    var a = $("#tt1").val();
    var b = $("#tt2").val();
    var c = $("#tt3").val();
    ntt = (parseInt(a) + parseInt(b) + parseInt(c)) /3;
    ntt = ntt.toFixed(2);
    $("#ntt").val(ntt);
    var d = $("#ntt").val();
    var e = $("#np").val();
    f =  (parseInt(e)*3 + parseInt(d)*7) /10 
    $("#na").val(f);           
}
<table class="table table-hover">
<tr>  
  <th>No</th> 						
  <th>NAMA</th>			
  <th>TOTAL HADIR</th>
  <th>TT 1</th>
  <th>TT 2</th>
  <th>TT 3</th>
  <th>N.RATA"</th>
  <th>N.PARTISIPASI</th>
  <th>N.AKHIR</th>
</tr>
<?php  
   if(isset($ambil_data)>0){$i=1; foreach($ambil_data as $row)  {  
		?>  
	<tr>  
		<td><?php echo $i; ?></td> 
		<td><?php echo $row->nama; ?></td>
		<td><input type="text" class="form-control" id="jml_hadir" maxlength="1" name="jml_hadir" placeholder="Hadir" /><?php echo form_error('jml_hadir');  ?></td>
		<td><input type="text" class="form-control" id="tt1" placeholder="TT1" maxlength="5" name="tt1" onkeyup="hitung2()" /><?php echo form_error('tt1');  ?></td>
		<td><input type="text" class="form-control" id="tt2" placeholder="TT1" maxlength="5" name="tt2" onkeyup="hitung2()" /><?php echo form_error('tt2');  ?></td>
		<td><input type="text" class="form-control" id="tt3" placeholder="TT1" maxlength="5" name="tt3" onkeyup="hitung2()" /><?php echo form_error('tt3');  ?></td>
		<td><input type="text" class="form-control" id="ntt" name="ntt" readonly /></td>
		<td><input type="text" class="form-control" id="np" placeholder="N.Partisipasi" name="np" onkeyup="hitung2()"/><?php echo form_error('np');  ?></td>
		<td><input type="text" class="form-control" id="na" name="na" readonly /></td>
	</tr>  
<?php $i++;}}?> 
</form>
</table>

2 个答案:

答案 0 :(得分:1)

首先,不要为多个dom元素分配相同的id。它与“ID”的含义相矛盾,而是使用类。

其次,将对象本身作为参数传递,以便您可以检测函数中的整行。

所以你的整个代码可以更新如下:

vec = Vectorize(function(L, U, level = .95){

p1 = (1 - level)/2
p2 = 1 - p1
f.beta <- function(alpha, beta, x, lower = 0, upper = 1){
 p <- pbeta((x-lower)/(upper-lower), alpha, beta)
 log(p/(1-p)) }

delta <- function(fit, actual) sum((fit-actual)^2)

objective <- function(theta, x, prob, ...) {
ab <- exp(theta)
fit <- f.beta(ab[1], ab[2], x, ...)
return (delta(fit, prob)) }

x.p <- (function(p) log(p/(1-p)))(c(p1, p2))

sol <- nlm(objective, log(c(1e1, 1e1)), x = c(L, U), prob = x.p, lower = 0, upper = 1, typsize = c(1, 1), fscale = 1e-12, gradtol = 1e-12)

parm <- as.numeric(exp(sol$estimate))

list(alpha = parm[[1]], beta = parm[[2]])

}, c("L", "U", "level"), SIMPLIFY = FALSE)

答案 1 :(得分:0)

传递this,如:

<input type="text" class="form-control" id="tt3" placeholder="TT1" maxlength="5" name="tt3" onkeyup="hitung2(this)" />

然后在功能:

function hitung2(thatObj) {
  ...................         
}

function hitung2(thatObj) {
    var el = $(thatObj).closest('tr');
    var a = el.find('input[name=tt1]').val() == "" ? 0: el.find('input[name=tt1]').val();
    var b = el.find('input[name=tt2]').val() == "" ? 0: el.find('input[name=tt2]').val();
    var c = el.find('input[name=tt3]').val() == "" ? 0: el.find('input[name=tt3]').val();

    let ntt = (parseInt(a) + parseInt(b) + parseInt(c)) /3;
    ntt = ntt.toFixed(2);

    el.find('input[name=ntt]').val(ntt);
    var d = el.find('input[name=ntt]').val() == "" ? 1: el.find('input[name=ntt]').val();
    var e = el.find('input[name=np]').val() == "" ? 1: el.find('input[name=np]').val();
    let f =  (parseInt(e)*3 + parseInt(d)*7) /10 
    el.find('input[name=na]').val(f);           
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<table class="table table-hover">
  <tr>
    <th>TT 1</th>
    <th>TT 2</th>
    <th>TT 3</th>
    <th>N.RATA</th>
    <th>N.PARTISIPASI</th>
    <th>N.AKHIR</th>
  </tr>
  <tr>
    <td><input type="text" class="form-control" id="tt1" placeholder="TT1" maxlength="5" name="tt1" onkeyup="hitung2(this)" /></td>
    <td><input type="text" class="form-control" id="tt2" placeholder="TT1" maxlength="5" name="tt2" onkeyup="hitung2(this)" /></td>
    <td><input type="text" class="form-control" id="tt3" placeholder="TT1" maxlength="5" name="tt3" onkeyup="hitung2(this)" /></td>
    <td><input type="text" class="form-control" id="ntt" name="ntt" readonly /></td>
    <td><input type="text" class="form-control" id="np" placeholder="N.Partisipasi" name="np" onkeyup="hitung2(this)"/></td>
    <td><input type="text" class="form-control" id="na" name="na" readonly /></td>
  </tr>
  <tr>
    <td><input type="text" class="form-control" id="tt1" placeholder="TT1" maxlength="5" name="tt1" onkeyup="hitung2(this)" /></td>
    <td><input type="text" class="form-control" id="tt2" placeholder="TT1" maxlength="5" name="tt2" onkeyup="hitung2(this)" /></td>
    <td><input type="text" class="form-control" id="tt3" placeholder="TT1" maxlength="5" name="tt3" onkeyup="hitung2(this)" /></td>
    <td><input type="text" class="form-control" id="ntt" name="ntt" readonly /></td>
    <td><input type="text" class="form-control" id="np" placeholder="N.Partisipasi" name="np" onkeyup="hitung2(this)"/></td>
    <td><input type="text" class="form-control" id="na" name="na" readonly /></td>
  </tr>
</table>