动态jQuery自动完成添加行

时间:2014-02-27 08:37:28

标签: javascript php jquery autocomplete

我有一个动态表..它工作但我有问题,当我必须在其中一个文本中使用自动完成时.. 我混淆了如何在自动完成中获取文本的ID。 对不起,我刚刚开始学习jQuery,我真的很困惑

<script type="text/javascript">
 function addpendfor(tname,it,rname,form)
 {
        rowtama++;
        addRow(tname,1,3,3,rowtama);

        document.getElementById('rowtam').value=rowtama;


 }

function addRow(tableID,pos_row,idcop,idcol,rowtam) 
 {

            var table = document.getElementById(tableID);
            var tablebody = table.tBodies[0];
            var rowCount = tablebody.rows.length;

                    var row = tablebody.insertRow(-1);      
                    var colCount = idcol;



                    for(var i=0; i<colCount; i++) {
                        var newcell = row.insertCell(i);            
                        newcell.innerHTML = tablebody.rows[pos_row].cells[i].innerHTML;

                         if(pos_row>idcop-1)
                            {
                                newcell.colSpan= colCount;
                                i=idcop;
                                if(pos_row==idcop)
                                    {
                                        newcell.style.borderBottom = '1px solid #E4E4E4';
                                    }
                            }


                        switch(newcell.childNodes[0].type) {

                            case "text":
                                    newcell.childNodes[0].name = newcell.childNodes[0].name+rowtam;
                                    newcell.childNodes[0].id = newcell.childNodes[0].id+rowtam;
                                    str=newcell.childNodes[0].name;
                                    break;
                            case "checkbox":
                                    newcell.childNodes[0].checked = false;
                                    newcell.childNodes[0].name = newcell.childNodes[0].name+rowtam;
                                    newcell.childNodes[0].id = newcell.childNodes[0].id+rowtam;
                                    break;
                            case "hidden":
                                    newcell.childNodes[0].name = newcell.childNodes[0].name+rowtam;
                                    newcell.childNodes[0].id = newcell.childNodes[0].id+rowtam;
                                    break;
                            default:
                                    newcell.childNodes[0].name = newcell.childNodes[0].name+rowtam;
                                    newcell.childNodes[0].id = newcell.childNodes[0].id+rowtam;
                                    break;
                        }                       
                    }
}

function delpendfor(tname,it,rname,form)
 {      
        deleteRow(tname,1);
 }

function deleteRow(tableID, idcop) 
{
            try {
            var table = document.getElementById(tableID);
            var tablebody = table.tBodies[0];

            var rowCount = tablebody.rows.length;

            for(var i=0; i<rowCount; i++) {
                var row = tablebody.rows[i];
                var chkbox = row.cells[0].childNodes[0];

                if( true == chkbox.checked) {

                            tablebody.deleteRow(i);
                            rowCount--;


                i--;    
                }

            }
            }catch(e) {
                alert(e);
            }
}

$(this).ready
(
 function() 
 {  
    $("#name").autocomplete({   
        minLength: 1,
        source: 
        function(req, add){
            $.ajax({
                url: "<?php echo base_url(); ?>index.php/data/get_new/",
                dataType: 'json',
                type: 'POST',
                data: req,
                success:    
                function(data){
                    if(data.response =="true"){
                        add(data.message);
                        }
                    },
                });
                },
                select: 
                function(event, ui) {
                    $("#kode").val(
                        ui.item.kode
                    );                                                          
                },  
    });
 }
);
</script> 


<table id="detail" class="table table-striped table-bordered">
<TR style="display:none">
    <TD><INPUT type="checkbox" name="chk" /></TD>
    <TD><INPUT type="text" name="name" id="name" /></TD>
    <TD><INPUT type="text" readonly name="kode" id="kode" value=""/></TD>                           
</TR>
</table>
 <a name="submit" class="btn btn-inverse" onClick="addpendfor('detail','','','')"  >Add</a>
 <a  class="btn" onClick="delpendfor('detail','','','')" >Delete</a>

0 个答案:

没有答案