基于另一个输入字段的自动填充输入字段OnKeyUp(动态生成)

时间:2014-10-06 03:31:39

标签: php jquery ajax

我希望今天我能为下面的案例找到合适的解决方案。

案例

实际上,我有动态输入字段,让我们说字段A,B和C.因此,目的是当字段A接收OnKeyUp Action时,字段B和C将自动填充来自字段A值的查询结果。

的index.php

 <html>
    <body>
    <script type="text/javascript">
        var ct = 1;

        // Function Tambah Form
        function new_link()
        {
            ct++;
            var div1 = document.createElement('div');
            div1.id = ct;

            // Link to Delete Form
            var delLink = '<div style="text-align:left"><a id="del" href="javascript:delIt('+ ct +')">Delete</a></div>';
            div1.innerHTML = document.getElementById('newlinktpl').innerHTML + delLink;
            document.getElementById('newlink').appendChild(div1);
        }
        // Function Delete Form
        function delIt(eleId)
        {
            d = document;

            var ele = d.getElementById(eleId);

            var parentEle = d.getElementById('newlink');

            parentEle.removeChild(ele);
        }

        function ajaxFunction(str,divId)
        {
            var httpxml;
            try
            {
                // Firefox, Opera 8.0+, Safari
                httpxml=new XMLHttpRequest();
            }
            catch (e)
            {
                // Internet Explorer
                try
                {
                    httpxml=new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch (e)
                {
                    try
                    {
                        httpxml=new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch (e)
                    {
                        alert("Your browser does not support AJAX!");
                        return false;
                    }
                }
            }
            function stateChanged()
            {
                if(httpxml.readyState==4)
                {
                    document.getElementById(divId).innerHTML=httpxml.responseText;
                }
            }
            var url="cek.php";
            url=url+"?txt="+str;
            url=url+"&sid="+Math.random();
            httpxml.onreadystatechange=stateChanged;
            httpxml.open("GET",url,true);
            httpxml.send(null);
        }

    </script>

    </head>
    <body>
    <form name="myForm">
    <a id="new" href="javascript:new_link()">ADD NEW PAYMENT</a>
    <div id="newlink">
    <table width="50%" border="1">
      <tr>
        <td><input type="text"
    onkeyup="ajaxFunction(this.value,'displayDiv');" name="username" /></td>
        <td>
        <div id="displayDiv">
        <table width="100%" border="0">
        <tr>
        <td><input type="text" name="curr"></td><td><input type="text" name="amount"></td>
        </tr>
        </table>
        </div>
        </td>
      </tr>
    </table>
    </div>
    </form>

    </body>
    </html>

    <div id="newlinktpl" style="display:none" >
    <div>
    <table width="50%" border="1">
      <tr>
        <td><input type="text"
    onkeyup="ajaxFunction(this.value,'displayDiv<?php echo ++$np;?>');" name="username" /></td>
        <td>
        <div id="displayDiv<?php echo $np;?>">
        <table width="100%" border="0">
        <tr>
        <td><input type="text" name="curr"></td><td><input type="text" name="amount"></td>
        </tr>
        </table>
        </div>
        </td>
      </tr>
    </table>
    </div> 
    </div>

cek.php

<?php
    mssql_connect('localhost','sa','');
    mssql_select_db('NCD');

    $in=$_GET['txt'];
    $msg="";
    $t=mssql_query("SELECT * FROM DPaym1 WHERE (JENIS = 1) AND (DPSPAID = 0) AND (CNDPSNO = '$in')");
    while($nt=mssql_fetch_array($t)){
        //$msg = "$nt[CURRID]#$nt[AMOUNT]";
        $curr = "$nt[CURRID]";
        $amount = "$nt[AMOUNT]";
    }
    //echo $msg;
    //echo "$curr&nbsp;$amount";
    ?>
    <table width="100%" border="0">
    <tr>
    <td><input type="text" name="curr" value="<?php echo $curr?>"></td>
    <td><input type="text" name="amount" value="<?php echo $amount?>"></td>
    </tr>
    </table>

上面的代码已经成功获得第1行。但是当我在第2行输入值(动态添加新行)时,结果更改第1行中的字段B和C而不是第2行。

0 个答案:

没有答案