如何根据onchange事件的序列ID名称获取值?

时间:2012-01-29 07:08:54

标签: php javascript onchange xmlhttprequest

有一个表包含添加行按钮的字段。我正在使用 onchange事件来调用其他表中的数据,因此当我在选项框中选择一个选项时,可以自动获取之前选择的值(do not need input the value manually). 然后我在同一个表中添加行,之后我在框选项中选择一个选项,并从第一个the problem is the values ​​obtained appear in the first box, not in the second box.

获取不同的值

对于html示例:

<table id="table">
    <tr>
        <td>No</td>
        <td>Name</td>
        <td>Class</td>
        <td>Point</td>
        <td>Action</td>
    </tr>
    <tr>
            <td>1</td>
        <td><select name="name">
            <option value="#">&nbsp;</option>
            <?php
                    opendb();
                $query = "SELECT * FROM student";
                $result = mysql_query($query);
                while($data = mysql_fetch_array($result)){
                    $ID = $data['ID'];
                    $Name = $data['Name'];
                    echo "<option value='$ID'>$Name</option>";
                }
            ?>
             </select>
        </td>
        <td><select name='class' onchange="choosepoint(this);">
            <option value="#">&nbsp;</option>
            <?php
                    opendb();
                $query = "SELECT * FROM student2";
                $result = mysql_query($query);
                while($data = mysql_fetch_array($result)){
                        $ID1 = $data['ID'];
                    $class = $data['Class'];
                    echo "<option value='$ID'>$class</option>";
                }
            ?>
            </select>
        </td>
        <td>
                <div id="point" name="point"></div>
            </td>
            <td><input type="button" value=" + " onClick="addRowToTable();"> | <input type="button" value=" - " onClick="removeRowFromTable();"></td>
    </tr>
</table>

我在搜索,我在这里找到的问题:

function choosepoint(combobox)
{
    var kode = combobox.value;
    if (!kode) return;
    xmlhttp.open('get', '../template/get_point.php?kode='+kode, true);
    xmlhttp.onreadystatechange = function() {
        if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200))
        {
             document.getElementById("point").innerHTML = xmlhttp.responseText;
        }
        return false;
    }
    xmlhttp.send(null);
}

如果ID名称“point”更改为“point2”,则该值将显示在第二个框中,但第一个框中的值丢失。

所以我尝试循环以获取ID名称的值为point,point2,point3等(取决于想要添加行的数量)。

function choosepoint(combobox)
{
    var kode = combobox.value;
    if (!kode) return;
    xmlhttp.open('get', '../template/get_point.php?kode='+kode, true);
    xmlhttp.onreadystatechange = function() {
        if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200))
        {
             Var tbl = document.getElementById("table");
             var r = tbl.length;
             var i = r;
             document.getElementById("point"+ i).innerHTML = xmlhttp.responseText;
        }
        return false;
    }
    xmlhttp.send(null);
}

我得到的结果是null或没有值,无法运行或没有得到值为point,point2,point3等。 我制作的剧本有错吗?需要一个很大的帮助m(_ _)m。

感谢。

更新:这是针对已解决的问题。

function choosepoint(combobox)
{
    var kode = combobox.value;
    if (!kode) return;
    xmlhttp.open('get', '../template/get_point.php?kode='+kode, true);
    xmlhttp.onreadystatechange = function() {
        if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200))
        {
            var tbl = document.getElementById("table");
        var r = tbl.rows.length;
            var add = document.getElementById("point"+r);

        add.innerHTML = xmlhttp.responseText;
        }
        return false;
    }
    xmlhttp.send(null);
}

1 个答案:

答案 0 :(得分:1)

在您的代码中,您将表格的ID称为“table”,然后您访问了“document.getElementById('table')”然后您拥有了使用“tbl.length”导致null或无值。这里的问题是,当您使用 getElementById 时,您没有“length”属性。要访问表的数量,请使用以下

<table name="table">
and then
tbl = document.getElementById('table')
r = tbl.rows.length; //This will give you the row length.
function choosepoint(combobox)
{
    var kode = combobox.value;
    if (!kode) return;
    xmlhttp.open('get', '../template/get_point.php?kode='+kode, true);
    xmlhttp.onreadystatechange = function() {
        if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200))
        {
             Var tbl = document.getElementById('table')
             var r = tbl.rows.length;
              alert(document.getElementById("point"+r));//This should be object. If undefined then you don't have any element.
              **//Edit Part**
              if(document.getElementById("point"+r))
                 document.getElementById("point"+r).innerHTML = xmlhttp.responseText;
              else
              {
                  var cellRight = row.insertCell(3);//The cell or column number can be any here am using it as third column.
                  var div = document.createElement('div');
                  div.id = 'point' + r;
                  cellRight.appendChild(div);
                  cellRight.innerHTML = xmlhttp.responseText;
              }
             **//Till Here**
        }
        return false;
    }
    xmlhttp.send(null);
}

希望这有帮助。