<SCRIPT language="javascript">
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[1].cells[i].innerHTML;
//alert(newcell.childNodes);
switch(newcell.childNodes[0].type) {
case "text":
newcell.childNodes[0].value = "";
break;
case "checkbox":
newcell.childNodes[0].checked = false;
break;
case "select-one":
newcell.childNodes[0].selectedIndex = 0;
break;
}
}
}
function deleteRow(tableID) {
try {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
if(rowCount <= 2) {
alert("Cannot delete all the rows.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
}catch(e) {
alert(e);
}
}
</SCRIPT>
<table id="items">
<INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />
<INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
<TABLE id="dataTable" border="0">
<tr class="header">
<th class="no">No</th>
<th class="kode">Kode </th>
<th class="skcd">Nama Barang</th>
<th class="sat">Satuan</th>
<th class="jml">Jumlah</th>
</tr>
<TR>
<TD><INPUT type="checkbox" name="chk[]"/></TD>
<TD><INPUT type="text" name="txtKode[]" id="txtKode_id[]"/></TD>
<?php
echo '<td >';
$result = mysql_query("select * from tabelsukucadang");
$jsArray = "var arrayNamaBarang= new Array();\n";
echo '<select name="cmbBarang[]" id="cmbBarangId[]" onchange="changeValue(this.value)">';
echo '<option>-Pilih Barang-</option>';
while ($row = mysql_fetch_array($result))
{
echo '<option value="' . $row['ID'] . '">' . $row['NAMA_BRG'].'</option>';
$jsArray .= "arrayNamaBarang['" . $row['ID'] . "'] = {Kode:'" . addslashes($row['KODE_BRG']) . "',Satuan:'".addslashes($row['SATUAN'])."'};\n";
}
echo '</select>';
echo '<script type="text/javascript">';
echo $jsArray;
echo 'function changeValue(id)';
echo '{ ';
echo 'document.getElementById("txtKode_id[]").value = arrayNamaBarang[id].Kode; ';
echo 'document.getElementById("txtSatuan_id[]").value = arrayNamaBarang[id].Satuan; ';
echo '}; ';
echo '</script>';
echo '</td>';
?>
<TD><INPUT type="text" name="txtSatuan[]" id="txtSatuan_id[]"/></TD>
<TD><INPUT type="text" name="txtJumlah[]"/></TD>
</TR>
</TABLE>
</table>
</form>
<TABLE id="dataTable" border="0">
<tr class="header">
<th class="no">No</th>
<th class="kode">Kode </th>
<th class="skcd">Nama Barang</th>
<th class="sat">Satuan</th>
<th class="jml">Jumlah</th>
</tr>
<TR>
<TD><INPUT type="checkbox" name="chk[]"/></TD>
<TD><INPUT type="text" name="txtKode[]" id="txtKode_id[]"/></TD>
<?php
echo '<td >';
$result = mysql_query("select * from tabelsukucadang");
$jsArray = "var arrayNamaBarang= new Array();\n";
echo '<select name="cmbBarang[]" id="cmbBarangId[]">';
echo '<option>-Pilih Barang-</option>';
while ($row = mysql_fetch_array($result))
{
echo '<option value="' . $row['ID'] . '">' . $row['NAMA_BRG'].'</option>';
$jsArray .= "arrayNamaBarang['" . $row['ID'] . "'] = {Kode:'" . addslashes($row['KODE_BRG']) . "',Satuan:'".addslashes($row['SATUAN'])."'};\n";
}
echo '</select>';
echo '</td>';
?>
<TD><INPUT type="text" name="txtSatuan[]" id="txtSatuan_id[]"/></TD>
<TD><INPUT type="text" name="txtJumlah[]"/></TD>
</TR>
</TABLE>
<!-- menambahkan jquery -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
<?php echo $jsArray;?>
$(function(){
$("#dataTable select").change(function(){
var id = $(this).val();
$(this).parent().prev().find('input').val(arrayNamaBarang[id].Kode);
$(this).parent().next().find('input').val(arrayNamaBarang[id].Satuan);
});
});
</script>
?>
<script type="text/javascript">
<?php echo $jsArray;?>
function addActionToSelect(){
$("#dataTable select").unbind();
$("#dataTable select").change(function(){
var id = $(this).val();
$(this).parent().prev().find('input').val(arrayNamaBarang[id].Kode);
$(this).parent().next().find('input').val(arrayNamaBarang[id].Satuan);
});
}
$(function(){
addActionToSelect();
});
</script>
dan pada function yang kamu buat yaitu function addRow() tambahkan addActionToSelect(); pada bagian akhir function. function addRow(tableID)
{
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++)
{
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[1].cells[i].innerHTML;
//alert(newcell.childNodes);
switch(newcell.childNodes[0].type)
{
case "text":
newcell.childNodes[0].value = "";
break;
case "checkbox":
newcell.childNodes[0].checked = false;
break;
case "select-one":
newcell.childNodes[0].selectedIndex = 0;
break;
}
}
addActionToSelect();
}
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[1].cells[i].innerHTML;
//alert(newcell.childNodes);
switch(newcell.childNodes[0].type) {
case "text":
newcell.childNodes[0].value = "";
break;
case "checkbox":
newcell.childNodes[0].checked = false;
break;
case "select-one":
newcell.childNodes[0].selectedIndex = 0;
break;
}
}
addActionToSelect();
}