无法读取null的属性值。 onclick没有价值

时间:2016-11-28 13:31:01

标签: javascript jquery html onclick

我有一个可以使用AJAX更新表格的网页。我在onlick函数上遇到了一些问题。

每当我点击它在控制台上显示的更新按钮

  

无法阅读财产'价值' of null(...)bb @ manageaccounts.php:184 onclick @ manageaccounts.php:229

我在update.php文件中检查了它,但代码很好。我尝试将onclick函数从onclick="(this.id)"更改为onclick="(this.firstname)"但没有效果。

manageaccounts.php

<body>
    <div class="wrapper">
        <div id="data" class="table"></div>
    </div>
    <script type="text/javascript">
        data();

        function data() {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open("GET", "update.php?status=disp", false);
            xmlhttp.send(null);
            document.getElementById("data").innerHTML = xmlhttp.responseText;
        }

        function aa(a) {
            firstid = "firstname" + a;
            txtfirstid = "txtfirst" + a;
            var firstname = document.getElementById(firstid).innerHTML;
            document.getElementById(firstid).innerHTML = "<input type='text' value='" + firstname + "' id='" + txtfirstid + "'>";

            midid = "middlename" + a;
            txtmidid = "txtmid" + a;
            var middlename = document.getElementById(midid).innerHTML;
            document.getElementById(midid).innerHTML = "<input type='text' value='" + middlename + "' id='" + txtmidid + "'>";

            lastid = "lastname" + a;
            txtlastid = "txtlast" + a;
            var lastname = document.getElementById(lastid).innerHTML;
            document.getElementById(lastid).innerHTML = "<input type='text' value='" + lastname + "' id='" + txtlastid + "'>";

            addid = "address" + a;
            txtaddid = "txtadd" + a;
            var address = document.getElementById(addid).innerHTML;
            document.getElementById(addid).innerHTML = "<input type='text' value='" + address + "' id='" + txtaddid + "'>";

            gendid = "gender" + a;
            txtgendid = "txtgend" + a;
            var gender = document.getElementById(gendid).innerHTML;
            document.getElementById(gendid).innerHTML = "<input type='text' value='" + gender + "' id='" + txtgendid + "'>";

            contid = "contact" + a;
            txtcontid = "txtcont" + a;
            var contact = document.getElementById(contid).innerHTML;
            document.getElementById(contid).innerHTML = "<input type='text' value='" + contact + "' id='" + txtcontid + "'>";

            updateid = "update" + a;
            document.getElementById(a).style.visibility = "hidden";
            document.getElementById(updateid).style.visibility = "visible";

        }

        function bb(b) {
            var firstid = "txtfirst" + b;
            var firstname = document.getElementById(firstid).value;

            var midid = "txtmid" + b;
            var middlename = document.getElementById(midid).value;

            var lastid = "txtlast" + b;
            var lastname = document.getElementById(lastid).value;

            var addid = "txtadd" + b;
            var address = document.getElementById(addid).value;

            var gendid = "txtgend" + b;
            var gender = document.getElementById(gendid).value;

            var contid = "txtcont" + b;
            var contact = document.getElementById(contid).value;

            update_value(b, firstname, middlename, lastname, address, gender, contact);

            document.getElementById(b).style.visibility = "visible";
            document.getElementById("update" + b).style.visibility = "hidden";

            document.getElementById("firstname" + b).innerHTML = firstname;
            document.getElementById("middlename" + b).innerHTML = middlename;
            document.getElementById("lastname" + b).innerHTML = lastname;
            document.getElementById("address" + b).innerHTML = address;
            document.getElementById("gender" + b).innerHTML = gender;
            document.getElementById("contact" + b).innerHTML = contact;
        }

        function update_value(id, firstname, middlename, lastname, address, gender, contact) {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open("GET", "update.php?id=" + id + " & firstname=" + firstname + " & middlename=" + middlename + " & lastname=" + lastname + " &address=" + address + " & gender=" + gender + " &contact=" + contact + " & status=update", false);
            xmlhttp.send(null);
        }

        function delete1(id) {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open("GET", "update.php?id=" + id + " & status=delete", false);
            xmlhttp.send(null);
            data();
        }
    </script>
</body>

update.php 的空值为on <input type="button" id="update<?php echo $row["id"]; ?>" name="<?php echo $row["id"]; ?>" value="UPDATE" style="visibility: hidden;" onclick="bb(this.firstname)">

<?php
    $conn = mysqli_connect('localhost', 'root','root', 'realestate');
    if (!$conn) {
        die("Sorry we're having some problems with the database. :(".mysqli_connect_error());
    }

    $status = $_GET["status"];    
    if ($status == "disp") {
        $sql1 = "SELECT * FROM agents"; // check the change ere
        $result=mysqli_query($conn, $sql1);

        echo "<table>";
        echo "<tr>
            <th>Agent ID</th>
            <th>Firstname</th>
            <th>Middlename</th>
            <th>Lastname</th>
            <th>Address</th>
            <th>Gender</th>
            <th>Contact</th>
            <th>Action</th>
        </tr>"; 

        while($row=mysqli_fetch_assoc($result)) {
            echo "<tr class='da'>";
            echo "<td>"; echo $row["id"]; echo "</td>";
            echo "<td>"; ?><div id="firstname<?php echo $row["id"]; ?>"><?php echo $row["firstname"]; ?></div><?php echo "</td>";
            echo "<td>"; ?><div id="middlename<?php echo $row["id"]; ?>"><?php echo $row["middlename"]; ?></div><?php echo "</td>";
            echo "<td>"; ?><div id="lastname<?php echo $row["id"]; ?>"><?php echo $row["lastname"]; ?></div><?php echo "</td>";
            echo "<td>"; ?><div id="address<?php echo $row["id"]; ?>"><?php echo $row["address"]; ?></div><?php echo "</td>";
            echo "<td>"; ?><div id="gender<?php echo $row["id"]; ?>"><?php echo $row["gender"]; ?></div><?php echo "</td>";
            echo "<td>"; ?><div id="contact<?php echo $row["id"]; ?>"><?php echo $row["contact"]; ?></div><?php echo "</td>";
            echo "<td>"; ?><button id="<?php echo $row["id"]; ?>" name="<?php echo $row["id"]; ?>" onclick="delete1(this.id)">DELETE</button><?php echo "</td>";
            echo "<td>"; ?>
            <input type="button" id="<?php echo $row["id"]; ?>" name="<?php echo $row["id"]; ?>" value="EDIT" onclick="aa(this.id)">
            <input type="button" id="update<?php echo $row["id"]; ?>" name="<?php echo $row["id"]; ?>" value="UPDATE"  style="visibility: hidden;" onclick="bb(this.firstname)">
        <?php echo "</td>";
        echo "</tr>";
    }
    echo "</table>";
}

if ($status == "update") {
    $id = $_GET["id"];
    $first = $_GET["firstname"];
    $mid = $_GET["middlename"];
    $last = $_GET["lastname"];
    $add = $_GET["address"];
    $gend = $_GET["gender"];
    $cont = $_GET["contact"];
    $first = trim($first);
    $mid = trim($mid);
    $last = trim($last);
    $add = trim($add);
    $gend = trim($gend);
    $cont = trim($cont);

     $sql2 = "UPDATE agents SET firstname='$first', middlename='$mid', lastname='$last', address='$add', gender='$gend', contact='$cont' WHERE id=$id";
        $result = mysqli_query($conn, $sql2);
    } 

    if ($status == "delete") {
        $id = $_GET["id"];
        $sql3 = "DELETE FROM agents WHERE id=$id";
        $result = mysqli_query($conn, $sql3);
    }
?>

1 个答案:

答案 0 :(得分:0)

在你的函数bb中,你正在做:

var firstid="txtfirst"+b;
var firstname = document.getElementById(firstid).value;

您正在调用以下函数:

onclick="bb(this.firstname)"

现在,this.firstname将是未定义的,因为this指向您的按钮,而该按钮没有名为firstname的属性。

此外,当你喜欢

onclick="(this.id)"

然后在您的函数中,firstid将变为“txtfirstupdate1”,因为您的按钮的ID将为update + id,而不仅仅是id。因此,document.getElementById(firstid)实际上是空的,这就是您收到此错误的原因。