attribut ID yang sama menjadi array

Tolong di Bantu, bagaimana membuat array dari ID yang sama untuk menampilkan hasil array menggunakan perulangan.

sa.JPG

hanya muncul hasil perulangan terakhir pada persentase sebelah kanan,

seharusnya muncul 20 dan 30 sesuai dengan barisnya

berikut codenya:

<head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<table>
 <tr>
    <td>Modal disetor</td>
    <td> <input onkeydown="return numbersonlysetor(this, event);" onkeyup="javascript:tandaPemisahTitiksetor(this);" required id="inputcompanymodal" name="inputcompanymodal" style="width:auto" type="text">
    <span id="pesansetoran"></span>
      <p name="demo" id="demo"></p></td></tr>
<tr>
    <td>Pemegang Saham</td>
    <td>

       <a class="btn btn-primary" style="color:white" onclick="myCalc()">hitung</a><span style="margin-left: 20px" id="pesanhurufsaham"></span>
      <div class="control-group after-add-more-saham"> <input onKeyPress="return ValidateAlpha(event);" required name="inputcompanysaham[]" placeholder="Nama Pemegang" type="text" style="width:auto" maxlength="35">
       <input required id="inputjumlahsetor" onkeydown="return numbersonly(this, event);" onkeyup="javascript:tandaPemisahTitik(this);"  name="inputjumlahsetor[]" style="width:auto" placeholder="Jumlah Setor"  type="text" maxlength="35">
     <button class="btn btn-primary add-more-saham" id="addmoresaham"  type="button">
              <i class="glyphicon glyphicon-plus"></i> +
            </button>
            <span id="persenan">%</span>
</div>

<div class="copysaham invisible">
            <div class="control-group">
                 <input onKeyPress="return ValidateAlpha(event);"  name="inputcompanysaham[]" placeholder="Nama Pemegang" type="text" style="width:auto" maxlength="35">
       <input id="inputjumlahsetor" onkeydown="return numbersonly(this, event);" onkeyup="javascript:tandaPemisahTitik(this);"  name="inputjumlahsetor[]" style="width:auto" placeholder="Jumlah Setor"  type="text" maxlength="35">
<button class="btn btn-danger remove" hidden="false" id="removesaham"   type="button"><i class="glyphicon glyphicon-remove"></i> X </button><span id="persenan">%</persenan>

            </div>
          </div>
<p  style="color:black" name="total" id="total"></p>


   </td></tr>

</table>

<script>
function myCalc(){

  var newArray = [];
$( "input[id='inputjumlahsetor']" ).each(function() {
    newArray.push($( this ).val().split('.').join(''));
});

var jumlaharraypersen = newArray.length-1;

var companymodal = document.getElementById("inputcompanymodal").value ;
    var jumlahsetor = document.getElementById("inputjumlahsetor").value ;
    var nama1 = parseInt(companymodal.split('.').join(''));
    var nama2 = parseInt(jumlahsetor.split('.').join(''));
var compare = newArray.reduce(myFunc);
for (var i = 0; i < jumlaharraypersen; i++) {
  var hasilarray = newArray[i]/nama1*100;
   document.getElementById('persenan').innerHTML = hasilarray + "%</b>";

}


var hasilnya = compare/nama1*100;

document.getElementById('total').innerHTML = "<b>Total: "+ hasilnya + "%</b>";
if(compare != nama1){
document.getElementById("demo").innerHTML = "<b style='color:red;'>" + "Jumlah Setoran Tidak Sesuai dengan modal" +"</b>";

}else{
  var pesan = document.getElementById("companyname").value.toUpperCase();
var contoh1 = pesan.substr(0,2);

if(contoh1 == "PT")
{
document.getElementById("demo").innerHTML = "<b style='color:green;'>" + "Setoran Sudah Sesuai" +"</b>";

}else{
  document.getElementById("demo").innerHTML = "<b style='color:green;'>" + "Setoran Sudah Sesuai" +"</b>";

}

}
 function myFunc(total, num) {
  return total - -num;
}
}

  function tandaPemisahTitiksetor(b){
var _minus = false;
if (b<0) _minus = true;
b = b.toString();
b=b.replace(".","");
b=b.replace("-","");
c = "";
panjang = b.length;
j = 0;
for (i = panjang; i > 0; i--){
j = j + 1;
if (((j % 3) == 1) && (j != 1)){
c = b.substr(i-1,1) + "." + c;
} else {
c = b.substr(i-1,1) + c;
}
}
if (_minus) c = "-" + c ;
return c;
}
function numbersonlysetor(ini, e){
if (e.keyCode>=49){
if(e.keyCode<=57){
a = ini.value.toString().replace(".","");
b = a.replace(/[^\d]/g,"");
b = (b=="0")?String.fromCharCode(e.keyCode):b + String.fromCharCode(e.keyCode);
ini.value = tandaPemisahTitik(b);
return false;
}
else if(e.keyCode<=105){
if(e.keyCode>=96){
//e.keycode = e.keycode - 47;
a = ini.value.toString().replace(".","");
b = a.replace(/[^\d]/g,"");
b = (b=="0")?String.fromCharCode(e.keyCode-48):b + String.fromCharCode(e.keyCode-48);
ini.value = tandaPemisahTitik(b);
//alert(e.keycode);
return false;
}
else {return false;}
}
else {
return false; }
}else if (e.keyCode==48){
a = ini.value.replace(".","") + String.fromCharCode(e.keyCode);
b = a.replace(/[^\d]/g,"");
if (parseFloat(b)!=0){
ini.value = tandaPemisahTitik(b);
return false;
} else {
return false;
}
}else if (e.keyCode==95){
a = ini.value.replace(".","") + String.fromCharCode(e.keyCode-48);
b = a.replace(/[^\d]/g,"");
if (parseFloat(b)!=0){
ini.value = tandaPemisahTitik(b);
return false;
} else {
return false;
}
}else if (e.keyCode==8 || e.keycode==46){
a = ini.value.replace(".","");
b = a.replace(/[^\d]/g,"");
b = b.substr(0,b.length -1);
if (tandaPemisahTitik(b)!=""){
ini.value = tandaPemisahTitik(b);
} else {
ini.value = "";
}

return false;
} else if (e.keyCode==9){
return true;
} else if (e.keyCode==17){
return true;
} else {
//alert (e.keyCode);
return false;
}

}
</script>
 <script src="js/jquery-3.3.1.min.js"></script>
    <script src="js/popper.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/jquery.sticky.js"></script>
    <script src="js/main.js"></script>
    <script src="js/bootstrap-datepicker.js"></script>
avatar michaelwillieson
@michaelwillieson

3 Kontribusi 2 Poin

Diperbarui 2 tahun yang lalu

1 Jawaban:

Jawaban Terpilih

<div>menggunakan getElementsByName<br><br></div><pre>for (var i = 0; i &lt; jumlaharraypersen; i++) { var hasilarray = newArray[i]/nama1*100; document.getElementsByName("person")[i].innerHTML = hasilarray + "%"; } </pre>

avatar michaelwillieson
@michaelwillieson

3 Kontribusi 2 Poin

Dipost 2 tahun yang lalu

Login untuk ikut Jawaban