php和javascript插入行并给出提示

时间:2013-10-08 12:38:26

标签: javascript php

我想给每一行提示,现在我有问题

如何将行号放入此中 div id="txtHint"成为 txthint1 * txthint2 * txthint3

onchange="showUser(this.value,i)这是行数... 所以我可以使用showuser功能

JS:

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[0].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 <= 1) {
                    alert("Cannot delete all the rows.");
                    break;
                }
                table.deleteRow(i);
                rowCount--;
                i--;
            }


        }
    }catch(e) {
        alert(e);
    }
}
function showUser(str)
{
    if (str=="")
    {
        document.getElementById("txtHint").innerHTML="";
        return;
    } 
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","getuser.php?q="+str,true);
    xmlhttp.send();
}

HTML:

<INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />

<INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
<form action="http://localhost/tes/index.php/form/prints" method="post">
<TABLE id="dataTable" width="350px" border="1">
    <TR>
        <TD><INPUT type="checkbox" name="chk[]"/></TD>
        <TD><select name="a[]" id="a" onchange="showUser(this.value)">
        <option value="">select</option>
      <?php
    $sql="select * FROM user";

    $result = mysql_query($sql);        

    while($row = mysql_fetch_array($result))
      {

      ?>
        <option value="<?=$row['username']?>"><?=$row['username']?></option>
    <?php
    }
    ?>


            </select>
        </TD>
        <TD>
            <div id="txtHint"></div>
        </TD>
    </TR>
</TABLE>
<input type="submit"/>
</form>

1 个答案:

答案 0 :(得分:0)

您基本上处理的是事件注册问题。您可以将数据存储在ID中(您提到的i值)。但是你很难把它取回来,是吗?

请在此处阅读此主题:http://www.quirksmode.org/js/events_advanced.html

然后,一旦你得到(基本上)尝试这样的事情:

function registerEvents() {
    var showUser = function(e) {
        if (!e) var e = window.event;
        var target = e.target || e.srcElement;
        if (target.nodeType == 3) // defeat Safari bug
            target = target.parentNode;
        console.log(target);
        //  Your code would go here
        //  ..probably in place of the console.log() line as well.
    },
        eventElements = document.getElementsByTagName("select"),
        eventRegistrar;
    if(eventElements.length > 0) {
        for(var i=0;i<eventElements.lengthli++) {
            eventRegistrar = eventElements[i].addEventListener || eventElements[i].attachEvent;
            eventRegistrar(((eventRegistrar==eventElements[i].attachEvent)?"on":"")+"change", registerEvents);
        }
    }
}
var windowEventRegistrar = window.addEventListener || window.attachEvent;
windowEventRegistrar(((windowEventRegistrar==window.attachEvent)?"on":"")+"load", registerEvents);

或者您可以使用jQuery,这会为您编写更少的代码。